<!DOCTYPE html><html lang="zh-Hans"><head><meta charset="utf-8"><title>Angular - 结构型指令</title><meta name="Description" content="Angular is a platform for building mobile and desktop web applications.
    Join the community of millions of developers who build compelling user interfaces with Angular."><base href="/"><meta name="viewport" content="width=device-width,initial-scale=1"><link rel="search" type="application/opensearchdescription+xml" href="assets/opensearch.xml"><link rel="icon" type="image/x-icon" href="assets/images/favicons/favicon.ico"><link rel="icon" type="image/png" href="assets/images/favicons/favicon-32x32.png" sizes="32x32"><link rel="icon" type="image/png" href="assets/images/favicons/favicon-194x194.png" sizes="194x194"><link rel="icon" type="image/png" href="assets/images/favicons/favicon-96x96.png" sizes="96x96"><link rel="icon" type="image/png" href="assets/images/favicons/favicon-16x16.png" sizes="16x16"><link rel="apple-touch-icon" sizes="144x144" href="assets/images/favicons/favicon-144x144.png"><link rel="apple-touch-icon-precomposed" sizes="144x144" href="assets/images/favicons/favicon-144x144.png"><link href="assets/fonts/Material_Icons.css" rel="stylesheet"><link href="assets/fonts/Droid_Sans_Mono.css" rel="stylesheet"><link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"><link rel="manifest" href="pwa-manifest.json"><meta name="theme-color" content="#1976d2"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-status-bar-style" content="translucent"><script async="" src="assets/js/analytics.js"></script><script>!function(e,a,n,t,s,c,g){e.GoogleAnalyticsObject=s,e.ga=e.ga||function(){(e.ga.q=e.ga.q||[]).push(arguments)},e.ga.l=1*new Date,c=a.createElement(n),g=a.getElementsByTagName(n)[0],c.async=1,c.src="assets/js/analytics.js",~e.name.indexOf("NG_DEFER_BOOTSTRAP")||g.parentNode.insertBefore(c,g)}(window,document,"script",0,"ga")</script><script>window.onerror=function(){ga("send","exception",{exDescription:function(e,r,n,a,c){var l;e=e.replace(/^Error: /,""),l=c?c.stack.replace(/^Error: /,"").replace(e+"\n","").replace(/^ +/gm,"").replace(/^at /gm,"").replace(/(?: \(|@)http.+\/([^/)]+)\)?(?:\n|$)/gm,"@$1\n").replace(/ *\(eval code(:\d+:\d+)\)(?:\n|$)/gm,"@???$1\n"):r+":"+(n=n||"?")+":"+(a=a||"?");return(e+"\n"+l).substr(0,150)}.apply(null,arguments),exFatal:!0})}</script><link rel="stylesheet" href="styles.4adbe52ad34df01b5273.css"><style>.cdk-high-contrast-active .mat-toolbar{outline:solid 1px}.mat-toolbar-row,.mat-toolbar-single-row{display:flex;box-sizing:border-box;padding:0 16px;width:100%;flex-direction:row;align-items:center;white-space:nowrap}.mat-toolbar-multiple-rows{display:flex;box-sizing:border-box;flex-direction:column;width:100%}.mat-toolbar-multiple-rows{min-height:64px}.mat-toolbar-row,.mat-toolbar-single-row{height:64px}@media(max-width:599px){.mat-toolbar-multiple-rows{min-height:56px}.mat-toolbar-row,.mat-toolbar-single-row{height:56px}}</style><style>.mat-icon{background-repeat:no-repeat;display:inline-block;fill:currentColor;height:24px;width:24px}.mat-icon.mat-icon-inline{font-size:inherit;height:inherit;line-height:inherit;width:inherit}[dir=rtl] .mat-icon-rtl-mirror{transform:scale(-1,1)}.mat-form-field:not(.mat-form-field-appearance-legacy) .mat-form-field-prefix .mat-icon,.mat-form-field:not(.mat-form-field-appearance-legacy) .mat-form-field-suffix .mat-icon{display:block}.mat-form-field:not(.mat-form-field-appearance-legacy) .mat-form-field-prefix .mat-icon-button .mat-icon,.mat-form-field:not(.mat-form-field-appearance-legacy) .mat-form-field-suffix .mat-icon-button .mat-icon{margin:auto}</style><style>.mat-button .mat-button-focus-overlay,.mat-icon-button .mat-button-focus-overlay{opacity:0}.mat-button:hover .mat-button-focus-overlay,.mat-stroked-button:hover .mat-button-focus-overlay{opacity:.04}@media(hover:none){.mat-button:hover .mat-button-focus-overlay,.mat-stroked-button:hover .mat-button-focus-overlay{opacity:0}}.mat-button,.mat-flat-button,.mat-icon-button,.mat-stroked-button{box-sizing:border-box;position:relative;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;cursor:pointer;outline:0;border:none;-webkit-tap-highlight-color:transparent;display:inline-block;white-space:nowrap;text-decoration:none;vertical-align:baseline;text-align:center;margin:0;min-width:64px;line-height:36px;padding:0 16px;border-radius:4px;overflow:visible}.mat-button::-moz-focus-inner,.mat-flat-button::-moz-focus-inner,.mat-icon-button::-moz-focus-inner,.mat-stroked-button::-moz-focus-inner{border:0}.mat-button[disabled],.mat-flat-button[disabled],.mat-icon-button[disabled],.mat-stroked-button[disabled]{cursor:default}.mat-button.cdk-keyboard-focused .mat-button-focus-overlay,.mat-button.cdk-program-focused .mat-button-focus-overlay,.mat-flat-button.cdk-keyboard-focused .mat-button-focus-overlay,.mat-flat-button.cdk-program-focused .mat-button-focus-overlay,.mat-icon-button.cdk-keyboard-focused .mat-button-focus-overlay,.mat-icon-button.cdk-program-focused .mat-button-focus-overlay,.mat-stroked-button.cdk-keyboard-focused .mat-button-focus-overlay,.mat-stroked-button.cdk-program-focused .mat-button-focus-overlay{opacity:.12}.mat-button::-moz-focus-inner,.mat-flat-button::-moz-focus-inner,.mat-icon-button::-moz-focus-inner,.mat-stroked-button::-moz-focus-inner{border:0}.mat-raised-button{box-sizing:border-box;position:relative;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;cursor:pointer;outline:0;border:none;-webkit-tap-highlight-color:transparent;display:inline-block;white-space:nowrap;text-decoration:none;vertical-align:baseline;text-align:center;margin:0;min-width:64px;line-height:36px;padding:0 16px;border-radius:4px;overflow:visible;transform:translate3d(0,0,0);transition:background .4s cubic-bezier(.25,.8,.25,1),box-shadow 280ms cubic-bezier(.4,0,.2,1)}.mat-raised-button::-moz-focus-inner{border:0}.mat-raised-button[disabled]{cursor:default}.mat-raised-button.cdk-keyboard-focused .mat-button-focus-overlay,.mat-raised-button.cdk-program-focused .mat-button-focus-overlay{opacity:.12}.mat-raised-button::-moz-focus-inner{border:0}._mat-animation-noopable.mat-raised-button{transition:none;animation:none}.mat-stroked-button{border:1px solid currentColor;padding:0 15px;line-height:34px}.mat-stroked-button .mat-button-focus-overlay,.mat-stroked-button .mat-button-ripple.mat-ripple{top:-1px;left:-1px;right:-1px;bottom:-1px}.mat-fab{box-sizing:border-box;position:relative;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;cursor:pointer;outline:0;border:none;-webkit-tap-highlight-color:transparent;display:inline-block;white-space:nowrap;text-decoration:none;vertical-align:baseline;text-align:center;margin:0;min-width:64px;line-height:36px;padding:0 16px;border-radius:4px;overflow:visible;transform:translate3d(0,0,0);transition:background .4s cubic-bezier(.25,.8,.25,1),box-shadow 280ms cubic-bezier(.4,0,.2,1);min-width:0;border-radius:50%;width:56px;height:56px;padding:0;flex-shrink:0}.mat-fab::-moz-focus-inner{border:0}.mat-fab[disabled]{cursor:default}.mat-fab.cdk-keyboard-focused .mat-button-focus-overlay,.mat-fab.cdk-program-focused .mat-button-focus-overlay{opacity:.12}.mat-fab::-moz-focus-inner{border:0}._mat-animation-noopable.mat-fab{transition:none;animation:none}.mat-fab .mat-button-wrapper{padding:16px 0;display:inline-block;line-height:24px}.mat-mini-fab{box-sizing:border-box;position:relative;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;cursor:pointer;outline:0;border:none;-webkit-tap-highlight-color:transparent;display:inline-block;white-space:nowrap;text-decoration:none;vertical-align:baseline;text-align:center;margin:0;min-width:64px;line-height:36px;padding:0 16px;border-radius:4px;overflow:visible;transform:translate3d(0,0,0);transition:background .4s cubic-bezier(.25,.8,.25,1),box-shadow 280ms cubic-bezier(.4,0,.2,1);min-width:0;border-radius:50%;width:40px;height:40px;padding:0;flex-shrink:0}.mat-mini-fab::-moz-focus-inner{border:0}.mat-mini-fab[disabled]{cursor:default}.mat-mini-fab.cdk-keyboard-focused .mat-button-focus-overlay,.mat-mini-fab.cdk-program-focused .mat-button-focus-overlay{opacity:.12}.mat-mini-fab::-moz-focus-inner{border:0}._mat-animation-noopable.mat-mini-fab{transition:none;animation:none}.mat-mini-fab .mat-button-wrapper{padding:8px 0;display:inline-block;line-height:24px}.mat-icon-button{padding:0;min-width:0;width:40px;height:40px;flex-shrink:0;line-height:40px;border-radius:50%}.mat-icon-button .mat-icon,.mat-icon-button i{line-height:24px}.mat-button-focus-overlay,.mat-button-ripple.mat-ripple{top:0;left:0;right:0;bottom:0;position:absolute;pointer-events:none;border-radius:inherit}.mat-button-ripple.mat-ripple:not(:empty){transform:translateZ(0)}.mat-button-focus-overlay{opacity:0;transition:opacity .2s cubic-bezier(.35,0,.25,1),background-color .2s cubic-bezier(.35,0,.25,1)}._mat-animation-noopable .mat-button-focus-overlay{transition:none}.cdk-high-contrast-active .mat-button-focus-overlay{background-color:#fff}.cdk-high-contrast-black-on-white .mat-button-focus-overlay{background-color:#000}.mat-button-ripple-round{border-radius:50%;z-index:1}.mat-button .mat-button-wrapper>*,.mat-fab .mat-button-wrapper>*,.mat-flat-button .mat-button-wrapper>*,.mat-icon-button .mat-button-wrapper>*,.mat-mini-fab .mat-button-wrapper>*,.mat-raised-button .mat-button-wrapper>*,.mat-stroked-button .mat-button-wrapper>*{vertical-align:middle}.mat-form-field:not(.mat-form-field-appearance-legacy) .mat-form-field-prefix .mat-icon-button,.mat-form-field:not(.mat-form-field-appearance-legacy) .mat-form-field-suffix .mat-icon-button{display:block;font-size:inherit;width:2.5em;height:2.5em}.cdk-high-contrast-active .mat-button,.cdk-high-contrast-active .mat-fab,.cdk-high-contrast-active .mat-flat-button,.cdk-high-contrast-active .mat-icon-button,.cdk-high-contrast-active .mat-mini-fab,.cdk-high-contrast-active .mat-raised-button{outline:solid 1px}</style><style>.mat-drawer-container{position:relative;z-index:1;box-sizing:border-box;-webkit-overflow-scrolling:touch;display:block;overflow:hidden}.mat-drawer-container[fullscreen]{top:0;left:0;right:0;bottom:0;position:absolute}.mat-drawer-container[fullscreen].mat-drawer-container-has-open{overflow:hidden}.mat-drawer-container.mat-drawer-container-explicit-backdrop .mat-drawer-side{z-index:3}.mat-drawer-container.ng-animate-disabled .mat-drawer-backdrop,.mat-drawer-container.ng-animate-disabled .mat-drawer-content,.ng-animate-disabled .mat-drawer-container .mat-drawer-backdrop,.ng-animate-disabled .mat-drawer-container .mat-drawer-content{transition:none}.mat-drawer-backdrop{top:0;left:0;right:0;bottom:0;position:absolute;display:block;z-index:3;visibility:hidden}.mat-drawer-backdrop.mat-drawer-shown{visibility:visible}.mat-drawer-transition .mat-drawer-backdrop{transition-duration:.4s;transition-timing-function:cubic-bezier(.25,.8,.25,1);transition-property:background-color,visibility}.cdk-high-contrast-active .mat-drawer-backdrop{opacity:.5}.mat-drawer-content{position:relative;z-index:1;display:block;height:100%;overflow:auto}.mat-drawer-transition .mat-drawer-content{transition-duration:.4s;transition-timing-function:cubic-bezier(.25,.8,.25,1);transition-property:transform,margin-left,margin-right}.mat-drawer{position:relative;z-index:4;display:block;position:absolute;top:0;bottom:0;z-index:3;outline:0;box-sizing:border-box;overflow-y:auto;transform:translate3d(-100%,0,0)}.cdk-high-contrast-active .mat-drawer,.cdk-high-contrast-active [dir=rtl] .mat-drawer.mat-drawer-end{border-right:solid 1px currentColor}.cdk-high-contrast-active .mat-drawer.mat-drawer-end,.cdk-high-contrast-active [dir=rtl] .mat-drawer{border-left:solid 1px currentColor;border-right:none}.mat-drawer.mat-drawer-side{z-index:2}.mat-drawer.mat-drawer-end{right:0;transform:translate3d(100%,0,0)}[dir=rtl] .mat-drawer{transform:translate3d(100%,0,0)}[dir=rtl] .mat-drawer.mat-drawer-end{left:0;right:auto;transform:translate3d(-100%,0,0)}.mat-drawer-inner-container{width:100%;height:100%;overflow:auto;-webkit-overflow-scrolling:touch}.mat-sidenav-fixed{position:fixed}</style><style>.nav-link.highlight[_ngcontent-ng-docs-c30]{color:#ff0}</style><style>.mat-progress-bar{display:block;height:4px;overflow:hidden;position:relative;transition:opacity 250ms linear;width:100%}._mat-animation-noopable.mat-progress-bar{transition:none;animation:none}.mat-progress-bar .mat-progress-bar-element,.mat-progress-bar .mat-progress-bar-fill::after{height:100%;position:absolute;width:100%}.mat-progress-bar .mat-progress-bar-background{width:calc(100% + 10px)}.cdk-high-contrast-active .mat-progress-bar .mat-progress-bar-background{display:none}.mat-progress-bar .mat-progress-bar-buffer{transform-origin:top left;transition:transform 250ms ease}.cdk-high-contrast-active .mat-progress-bar .mat-progress-bar-buffer{border-top:solid 5px;opacity:.5}.mat-progress-bar .mat-progress-bar-secondary{display:none}.mat-progress-bar .mat-progress-bar-fill{animation:none;transform-origin:top left;transition:transform 250ms ease}.cdk-high-contrast-active .mat-progress-bar .mat-progress-bar-fill{border-top:solid 4px}.mat-progress-bar .mat-progress-bar-fill::after{animation:none;content:"";display:inline-block;left:0}.mat-progress-bar[dir=rtl],[dir=rtl] .mat-progress-bar{transform:rotateY(180deg)}.mat-progress-bar[mode=query]{transform:rotateZ(180deg)}.mat-progress-bar[mode=query][dir=rtl],[dir=rtl] .mat-progress-bar[mode=query]{transform:rotateZ(180deg) rotateY(180deg)}.mat-progress-bar[mode=indeterminate] .mat-progress-bar-fill,.mat-progress-bar[mode=query] .mat-progress-bar-fill{transition:none}.mat-progress-bar[mode=indeterminate] .mat-progress-bar-primary,.mat-progress-bar[mode=query] .mat-progress-bar-primary{-webkit-backface-visibility:hidden;backface-visibility:hidden;animation:mat-progress-bar-primary-indeterminate-translate 2s infinite linear;left:-145.166611%}.mat-progress-bar[mode=indeterminate] .mat-progress-bar-primary.mat-progress-bar-fill::after,.mat-progress-bar[mode=query] .mat-progress-bar-primary.mat-progress-bar-fill::after{-webkit-backface-visibility:hidden;backface-visibility:hidden;animation:mat-progress-bar-primary-indeterminate-scale 2s infinite linear}.mat-progress-bar[mode=indeterminate] .mat-progress-bar-secondary,.mat-progress-bar[mode=query] .mat-progress-bar-secondary{-webkit-backface-visibility:hidden;backface-visibility:hidden;animation:mat-progress-bar-secondary-indeterminate-translate 2s infinite linear;left:-54.888891%;display:block}.mat-progress-bar[mode=indeterminate] .mat-progress-bar-secondary.mat-progress-bar-fill::after,.mat-progress-bar[mode=query] .mat-progress-bar-secondary.mat-progress-bar-fill::after{-webkit-backface-visibility:hidden;backface-visibility:hidden;animation:mat-progress-bar-secondary-indeterminate-scale 2s infinite linear}.mat-progress-bar[mode=buffer] .mat-progress-bar-background{-webkit-backface-visibility:hidden;backface-visibility:hidden;animation:mat-progress-bar-background-scroll 250ms infinite linear;display:block}.mat-progress-bar._mat-animation-noopable .mat-progress-bar-background,.mat-progress-bar._mat-animation-noopable .mat-progress-bar-buffer,.mat-progress-bar._mat-animation-noopable .mat-progress-bar-fill,.mat-progress-bar._mat-animation-noopable .mat-progress-bar-fill::after,.mat-progress-bar._mat-animation-noopable .mat-progress-bar-primary,.mat-progress-bar._mat-animation-noopable .mat-progress-bar-primary.mat-progress-bar-fill::after,.mat-progress-bar._mat-animation-noopable .mat-progress-bar-secondary,.mat-progress-bar._mat-animation-noopable .mat-progress-bar-secondary.mat-progress-bar-fill::after{animation:none;transition:none}@keyframes mat-progress-bar-primary-indeterminate-translate{0%{transform:translateX(0)}20%{animation-timing-function:cubic-bezier(.5,0,.701732,.495819);transform:translateX(0)}59.15%{animation-timing-function:cubic-bezier(.302435,.381352,.55,.956352);transform:translateX(83.67142%)}100%{transform:translateX(200.611057%)}}@keyframes mat-progress-bar-primary-indeterminate-scale{0%{transform:scaleX(.08)}36.65%{animation-timing-function:cubic-bezier(.334731,.12482,.785844,1);transform:scaleX(.08)}69.15%{animation-timing-function:cubic-bezier(.06,.11,.6,1);transform:scaleX(.661479)}100%{transform:scaleX(.08)}}@keyframes mat-progress-bar-secondary-indeterminate-translate{0%{animation-timing-function:cubic-bezier(.15,0,.515058,.409685);transform:translateX(0)}25%{animation-timing-function:cubic-bezier(.31033,.284058,.8,.733712);transform:translateX(37.651913%)}48.35%{animation-timing-function:cubic-bezier(.4,.627035,.6,.902026);transform:translateX(84.386165%)}100%{transform:translateX(160.277782%)}}@keyframes mat-progress-bar-secondary-indeterminate-scale{0%{animation-timing-function:cubic-bezier(.15,0,.515058,.409685);transform:scaleX(.08)}19.15%{animation-timing-function:cubic-bezier(.31033,.284058,.8,.733712);transform:scaleX(.457104)}44.15%{animation-timing-function:cubic-bezier(.4,.627035,.6,.902026);transform:scaleX(.72796)}100%{transform:scaleX(.08)}}@keyframes mat-progress-bar-background-scroll{to{transform:translateX(-8px)}}</style><script charset="utf-8" src="toc-toc-module-es2015.f8531f591ee147aebcc6.js"></script><script charset="utf-8" src="default~code-code-example-module~code-code-tabs-module-es2015.9efcc76d2979a2e605e0.js"></script><script charset="utf-8" src="code-code-example-module-es2015.fa05e6f700453813d256.js"></script><script charset="utf-8" src="code-code-tabs-module-es2015.505e1e0a869946918011.js"></script><script charset="utf-8" src="live-example-live-example-module-es2015.d3b124043392e69f9e97.js"></script><script charset="utf-8" src="assets-js-prettify-js-es2015.ba856648f2dd2d930a9b.js"></script><style>.mat-card{transition:box-shadow 280ms cubic-bezier(.4,0,.2,1);display:block;position:relative;padding:16px;border-radius:4px}._mat-animation-noopable.mat-card{transition:none;animation:none}.mat-card .mat-divider-horizontal{position:absolute;left:0;width:100%}[dir=rtl] .mat-card .mat-divider-horizontal{left:auto;right:0}.mat-card .mat-divider-horizontal.mat-divider-inset{position:static;margin:0}[dir=rtl] .mat-card .mat-divider-horizontal.mat-divider-inset{margin-right:0}.cdk-high-contrast-active .mat-card{outline:solid 1px}.mat-card-actions,.mat-card-content,.mat-card-subtitle{display:block;margin-bottom:16px}.mat-card-title{display:block;margin-bottom:8px}.mat-card-actions{margin-left:-8px;margin-right:-8px;padding:8px 0}.mat-card-actions-align-end{display:flex;justify-content:flex-end}.mat-card-image{width:calc(100% + 32px);margin:0 -16px 16px -16px}.mat-card-footer{display:block;margin:0 -16px -16px -16px}.mat-card-actions .mat-button,.mat-card-actions .mat-raised-button,.mat-card-actions .mat-stroked-button{margin:0 8px}.mat-card-header{display:flex;flex-direction:row}.mat-card-header .mat-card-title{margin-bottom:12px}.mat-card-header-text{margin:0 16px}.mat-card-avatar{height:40px;width:40px;border-radius:50%;flex-shrink:0;object-fit:cover}.mat-card-title-group{display:flex;justify-content:space-between}.mat-card-sm-image{width:80px;height:80px}.mat-card-md-image{width:112px;height:112px}.mat-card-lg-image{width:152px;height:152px}.mat-card-xl-image{width:240px;height:240px;margin:-8px}.mat-card-title-group>.mat-card-xl-image{margin:-8px 0 8px}@media(max-width:599px){.mat-card-title-group{margin:0}.mat-card-xl-image{margin-left:0;margin-right:0}}.mat-card-content>:first-child,.mat-card>:first-child{margin-top:0}.mat-card-content>:last-child:not(.mat-card-footer),.mat-card>:last-child:not(.mat-card-footer){margin-bottom:0}.mat-card-image:first-child{margin-top:-16px;border-top-left-radius:inherit;border-top-right-radius:inherit}.mat-card>.mat-card-actions:last-child{margin-bottom:-8px;padding-bottom:0}.mat-card-actions .mat-button:first-child,.mat-card-actions .mat-raised-button:first-child,.mat-card-actions .mat-stroked-button:first-child{margin-left:0;margin-right:0}.mat-card-subtitle:not(:first-child),.mat-card-title:not(:first-child){margin-top:-4px}.mat-card-header .mat-card-subtitle:not(:first-child){margin-top:-8px}.mat-card>.mat-card-xl-image:first-child{margin-top:-8px}.mat-card>.mat-card-xl-image:last-child{margin-bottom:-8px}</style><style>.mat-tab-group{display:flex;flex-direction:column}.mat-tab-group.mat-tab-group-inverted-header{flex-direction:column-reverse}.mat-tab-label{height:48px;padding:0 24px;cursor:pointer;box-sizing:border-box;opacity:.6;min-width:160px;text-align:center;display:inline-flex;justify-content:center;align-items:center;white-space:nowrap;position:relative}.mat-tab-label:focus{outline:0}.mat-tab-label:focus:not(.mat-tab-disabled){opacity:1}.cdk-high-contrast-active .mat-tab-label:focus{outline:dotted 2px}.mat-tab-label.mat-tab-disabled{cursor:default}.cdk-high-contrast-active .mat-tab-label.mat-tab-disabled{opacity:.5}.mat-tab-label .mat-tab-label-content{display:inline-flex;justify-content:center;align-items:center;white-space:nowrap}.cdk-high-contrast-active .mat-tab-label{opacity:1}@media(max-width:599px){.mat-tab-label{padding:0 12px}}@media(max-width:959px){.mat-tab-label{padding:0 12px}}.mat-tab-group[mat-stretch-tabs]>.mat-tab-header .mat-tab-label{flex-basis:0;flex-grow:1}.mat-tab-body-wrapper{position:relative;overflow:hidden;display:flex;transition:height .5s cubic-bezier(.35,0,.25,1)}._mat-animation-noopable.mat-tab-body-wrapper{transition:none;animation:none}.mat-tab-body{top:0;left:0;right:0;bottom:0;position:absolute;display:block;overflow:hidden;flex-basis:100%}.mat-tab-body.mat-tab-body-active{position:relative;overflow-x:hidden;overflow-y:auto;z-index:1;flex-grow:1}.mat-tab-group.mat-tab-group-dynamic-height .mat-tab-body.mat-tab-body-active{overflow-y:hidden}</style><style>.mat-tab-header{display:flex;overflow:hidden;position:relative;flex-shrink:0}.mat-tab-header-pagination{-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;position:relative;display:none;justify-content:center;align-items:center;min-width:32px;cursor:pointer;z-index:2;-webkit-tap-highlight-color:transparent;touch-action:none}.mat-tab-header-pagination-controls-enabled .mat-tab-header-pagination{display:flex}.mat-tab-header-pagination-before,.mat-tab-header-rtl .mat-tab-header-pagination-after{padding-left:4px}.mat-tab-header-pagination-before .mat-tab-header-pagination-chevron,.mat-tab-header-rtl .mat-tab-header-pagination-after .mat-tab-header-pagination-chevron{transform:rotate(-135deg)}.mat-tab-header-pagination-after,.mat-tab-header-rtl .mat-tab-header-pagination-before{padding-right:4px}.mat-tab-header-pagination-after .mat-tab-header-pagination-chevron,.mat-tab-header-rtl .mat-tab-header-pagination-before .mat-tab-header-pagination-chevron{transform:rotate(45deg)}.mat-tab-header-pagination-chevron{border-style:solid;border-width:2px 2px 0 0;content:"";height:8px;width:8px}.mat-tab-header-pagination-disabled{box-shadow:none;cursor:default}.mat-tab-list{flex-grow:1;position:relative;transition:transform .5s cubic-bezier(.35,0,.25,1)}.mat-ink-bar{position:absolute;bottom:0;height:2px;transition:.5s cubic-bezier(.35,0,.25,1)}._mat-animation-noopable.mat-ink-bar{transition:none;animation:none}.mat-tab-group-inverted-header .mat-ink-bar{bottom:auto;top:0}.cdk-high-contrast-active .mat-ink-bar{outline:solid 2px;height:0}.mat-tab-labels{display:flex}[mat-align-tabs=center] .mat-tab-labels{justify-content:center}[mat-align-tabs=end] .mat-tab-labels{justify-content:flex-end}.mat-tab-label-container{display:flex;flex-grow:1;overflow:hidden;z-index:1}._mat-animation-noopable.mat-tab-list{transition:none;animation:none}.mat-tab-label{height:48px;padding:0 24px;cursor:pointer;box-sizing:border-box;opacity:.6;min-width:160px;text-align:center;display:inline-flex;justify-content:center;align-items:center;white-space:nowrap;position:relative}.mat-tab-label:focus{outline:0}.mat-tab-label:focus:not(.mat-tab-disabled){opacity:1}.cdk-high-contrast-active .mat-tab-label:focus{outline:dotted 2px}.mat-tab-label.mat-tab-disabled{cursor:default}.cdk-high-contrast-active .mat-tab-label.mat-tab-disabled{opacity:.5}.mat-tab-label .mat-tab-label-content{display:inline-flex;justify-content:center;align-items:center;white-space:nowrap}.cdk-high-contrast-active .mat-tab-label{opacity:1}@media(max-width:599px){.mat-tab-label{min-width:72px}}</style><style>.mat-tab-body-content{height:100%;overflow:auto}.mat-tab-group-dynamic-height .mat-tab-body-content{overflow:hidden}</style></head><body><aio-shell ng-version="9.0.0-rc.11" class="aio-notification-hide folder-guide mode-stable page-guide-structural-directives sidenav-open view-SideNav"><div id="top-of-page"></div><mat-toolbar color="primary" class="mat-toolbar app-toolbar no-print mat-primary mat-toolbar-multiple-rows"><mat-toolbar-row class="mat-toolbar-row notification-container"><aio-notification notificationid="survey-february-2019" expirationdate="2019-03-01" class="ng-tns-c22-0 ng-trigger ng-trigger-hideAnimation" style="height:0"><span class="content ng-tns-c22-0"><a href="http://bit.ly/angular-survey-2019" target="_blank" class="ng-tns-c22-0"><mat-icon role="img" svgicon="insert_comment" aria-label="Announcement" class="mat-icon notranslate icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M20 2H4c-1.1 0-2 .9-2 2v12c0 1.1.9 2 2 2h14l4 4V4c0-1.1-.9-2-2-2zm-2 12H6v-2h12v2zm0-3H6V9h12v2zm0-3H6V6h12v2z"></path><path d="M0 0h24v24H0z" fill="none"></path></svg></mat-icon><span class="message"><b>填写这份《一分钟调查》</b>，帮我们（开发组）做得更好！</span><span class="action-button">去填写</span></a></span><button mat-icon-button="" aria-label="Close" class="close-button ng-tns-c22-0 mat-icon-button mat-button-base"><span class="mat-button-wrapper"><mat-icon role="img" svgicon="close" aria-label="Dismiss notification" class="mat-icon notranslate mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z"></path><path d="M0 0h24v24H0z" fill="none"></path></svg></mat-icon></span><div matripple="" class="mat-ripple mat-button-ripple mat-button-ripple-round"></div><div class="mat-button-focus-overlay"></div></button></aio-notification></mat-toolbar-row><mat-toolbar-row class="mat-toolbar-row"><button mat-button="" title="Docs menu" class="hamburger mat-button mat-button-base"><span class="mat-button-wrapper"><mat-icon role="img" svgicon="menu" class="mat-icon notranslate mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M3 18h18v-2H3v2zm0-5h18v-2H3v2zm0-7v2h18V6H3z"></path></svg></mat-icon></span><div matripple="" class="mat-ripple mat-button-ripple"></div><div class="mat-button-focus-overlay"></div></button><a href="/" class="nav-link home"><img src="assets/images/logos/angular/logo-nav@2x.png" width="150" height="40" title="Home" alt="Home" class="ng-star-inserted"></a><aio-top-menu _nghost-ng-docs-c30="" class="ng-star-inserted"><ul _ngcontent-ng-docs-c30="" role="navigation"><li _ngcontent-ng-docs-c30="" class="ng-star-inserted"><a _ngcontent-ng-docs-c30="" class="nav-link" href="features" title="特性" target="_self"><span _ngcontent-ng-docs-c30="" class="nav-link-inner">特性</span></a></li><li _ngcontent-ng-docs-c30="" class="ng-star-inserted"><a _ngcontent-ng-docs-c30="" class="nav-link" href="docs" title="文档" target="_self"><span _ngcontent-ng-docs-c30="" class="nav-link-inner">文档</span></a></li><li _ngcontent-ng-docs-c30="" class="ng-star-inserted"><a _ngcontent-ng-docs-c30="" class="nav-link" href="resources" title="资源" target="_self"><span _ngcontent-ng-docs-c30="" class="nav-link-inner">资源</span></a></li><li _ngcontent-ng-docs-c30="" class="ng-star-inserted"><a _ngcontent-ng-docs-c30="" class="nav-link" href="events" title="会议" target="_self"><span _ngcontent-ng-docs-c30="" class="nav-link-inner">会议</span></a></li><li _ngcontent-ng-docs-c30="" class="ng-star-inserted"><a _ngcontent-ng-docs-c30="" class="nav-link" href="https://blog.wangke.io/" title="译者汪志成的博客，包括翻译文章和原创文章" target="_blank"><span _ngcontent-ng-docs-c30="" class="nav-link-inner">译者博客</span></a></li><li _ngcontent-ng-docs-c30="" class="ng-star-inserted"><a _ngcontent-ng-docs-c30="" class="nav-link" href="translations/cn/home" title="关于中文版" target="_self"><span _ngcontent-ng-docs-c30="" class="nav-link-inner">关于中文版</span></a></li></ul></aio-top-menu><aio-search-box class="search-container"><input type="search" aria-label="search" placeholder="搜索"></aio-search-box><div class="toolbar-external-icons-container"><a href="https://twitter.com/angular" title="Twitter" aria-label="Angular on twitter"><mat-icon role="img" svgicon="logos:twitter" class="mat-icon notranslate mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 50 59" xmlns="http://www.w3.org/2000/svg"><path d="M50,9.3c-1.8,0.8-3.8,1.4-5.9,1.6c2.1-1.3,3.7-3.3,4.5-5.7c-2,1.2-4.2,2-6.5,2.5c-1.9-2-4.5-3.2-7.5-3.2c-5.7,0-10.3,4.6-10.3,10.3c0,0.8,0.1,1.6,0.3,2.3C16.1,16.7,8.5,12.6,3.5,6.4c-0.9,1.5-1.4,3.3-1.4,5.2c0,3.6,1.8,6.7,4.6,8.5C5,20,3.4,19.6,2,18.8c0,0,0,0.1,0,0.1c0,5,3.5,9.1,8.2,10.1c-0.9,0.2-1.8,0.4-2.7,0.4c-0.7,0-1.3-0.1-1.9-0.2c1.3,4.1,5.1,7,9.6,7.1c-3.5,2.8-7.9,4.4-12.7,4.4c-0.8,0-1.6,0-2.4-0.1c4.5,2.9,9.9,4.6,15.7,4.6c18.9,0,29.2-15.6,29.2-29.2c0-0.4,0-0.9,0-1.3C46.9,13.2,48.6,11.4,50,9.3z"></path></svg></mat-icon></a><a href="https://github.com/angular/angular" title="GitHub" aria-label="Angular on github"><mat-icon role="img" svgicon="logos:github" class="mat-icon notranslate mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 51.8 50.4" xmlns="http://www.w3.org/2000/svg"><path d="M25.9,0.2C11.8,0.2,0.3,11.7,0.3,25.8c0,11.3,7.3,20.9,17.5,24.3c1.3,0.2,1.7-0.6,1.7-1.2c0-0.6,0-2.6,0-4.8c-7.1,1.5-8.6-3-8.6-3c-1.2-3-2.8-3.7-2.8-3.7c-2.3-1.6,0.2-1.6,0.2-1.6c2.6,0.2,3.9,2.6,3.9,2.6c2.3,3.9,6,2.8,7.5,2.1c0.2-1.7,0.9-2.8,1.6-3.4c-5.7-0.6-11.7-2.8-11.7-12.7c0-2.8,1-5.1,2.6-6.9c-0.3-0.7-1.1-3.3,0.3-6.8c0,0,2.1-0.7,7,2.6c2-0.6,4.2-0.9,6.4-0.9c2.2,0,4.4,0.3,6.4,0.9c4.9-3.3,7-2.6,7-2.6c1.4,3.5,0.5,6.1,0.3,6.8c1.6,1.8,2.6,4.1,2.6,6.9c0,9.8-6,12-11.7,12.6c0.9,0.8,1.7,2.4,1.7,4.7c0,3.4,0,6.2,0,7c0,0.7,0.5,1.5,1.8,1.2c10.2-3.4,17.5-13,17.5-24.3C51.5,11.7,40.1,0.2,25.9,0.2z"></path></svg></mat-icon></a></div></mat-toolbar-row></mat-toolbar><mat-sidenav-container role="main" class="mat-drawer-container mat-sidenav-container sidenav-container mat-drawer-transition has-floating-toc"><div class="mat-drawer-backdrop ng-star-inserted"></div><div class="cdk-visually-hidden cdk-focus-trap-anchor" aria-hidden="true"></div><mat-sidenav tabindex="-1" class="mat-drawer mat-sidenav sidenav ng-tns-c18-1 ng-trigger ng-trigger-transform mat-drawer-side ng-star-inserted mat-drawer-opened" style="transform:none;visibility:visible"><div class="mat-drawer-inner-container ng-tns-c18-1"><aio-nav-menu class="ng-tns-c18-1"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-1 collapsed ng-star-inserted" style="position:relative" href="docs" title="Angular 文档简介" target="_self"><span>简介</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-1 collapsed ng-star-inserted" title="通过构建第一个 Angular 应用来学习基础知识" aria-pressed="false"><span>快速上手</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-1 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="start" title="介绍 Angular 的组件模型、模板语法和组件通讯" target="_self"><span>你的第一个应用</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="start/routing" title="介绍如何使用浏览器的 URL 在组件之间进行路由" target="_self"><span>路由</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="start/data" title="介绍服务以及如何访问外部数据" target="_self"><span>管理数据</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="start/forms" title="学习如何使用表单从用户那里获取并管理数据" target="_self"><span>表单</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="start/deployment" title="通过你的应用放到 Firebase 或自己的服务器上来把它分享给外界" target="_self"><span>部署</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-1 collapsed ng-star-inserted" style="position:relative" href="guide/setup-local" title="使用 Angular CLI 搭建本地开发环境简介" target="_self"><span>搭建环境</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-1 expanded selected ng-star-inserted" title="Angular 的基本原理" aria-pressed="true"><span>基本原理</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-1 expanded selected"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-2 collapsed ng-star-inserted" title="介绍 Angular 应用中的一些基本概念。" aria-pressed="false"><span>Angular 的基本概念</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-2 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/architecture" title="Angular 应用中的基本构造块。" target="_self"><span>基本概念简介</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/architecture-modules" title="关于 Angular 模块（NgModules）。" target="_self"><span>模块简介</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/architecture-components" title="关于组件、模板和视图。" target="_self"><span>组件简介</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/architecture-services" title="关于服务与依赖注入。" target="_self"><span>服务与 DI 简介</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/architecture-next-steps" title="超越基础阶段。" target="_self"><span>后续步骤</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-2 collapsed ng-star-inserted" title="英雄指南是这里大量 Angular 范例的基础" aria-pressed="false"><span>英雄指南</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-2 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="tutorial" title="《英雄指南》教程简介" target="_self"><span>简介</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="tutorial/toh-pt0" title="创建应用的外壳" target="_self"><span>应用的“外壳”</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="tutorial/toh-pt1" title="第一部分：构建一个简单的英雄编辑器" target="_self"><span>1. 英雄编辑器</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="tutorial/toh-pt2" title="第二部分：构建一个主从结构的页面，用于展现英雄列表。" target="_self"><span>2. 显示英雄列表</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="tutorial/toh-pt3" title="第三部分：把主从结构的视图重构成几个独立的组件。" target="_self"><span>3. 创建特性组件</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="tutorial/toh-pt4" title="第四部分：创建一个可复用的服务来管理英雄数据。" target="_self"><span>4. 添加服务</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="tutorial/toh-pt5" title="第五部分：添加 Angular 路由器，并且学习在视图之间导航。" target="_self"><span>5. 添加应用内导航</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="tutorial/toh-pt6" title="第六部分：通过 HTTP 来获取并保存英雄数据。" target="_self"><span>6. 从服务器获取数据</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-2 expanded selected ng-star-inserted" title="通过数据绑定构建动态视图" aria-pressed="true"><span>组件与模板</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-2 expanded selected"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/displaying-data" title="属性绑定可以帮助应用把数据显示在界面上" target="_self"><span>显示数据</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/template-syntax" title="学习如何写模板，以便借助数据绑定机制显示数据并响应事件。" target="_self"><span>模板语法</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/user-input" title="用户输入会触发 DOM 事件。Angular 会通过事件绑定来监听那些事件，并把修改后的值传回应用的组件和模型中。" target="_self"><span>用户输入</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/lifecycle-hooks" title="Angular 调用指令和组件的生命周期钩子函数，包括它的创建、变更和销毁时。" target="_self"><span>生命周期钩子</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/component-interaction" title="在不同的指令和组件之间共享信息" target="_self"><span>组件交互</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/component-styles" title="添加专属于某个组件的样式" target="_self"><span>组件样式</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/dynamic-component-loader" title="动态加载组件" target="_self"><span>动态组件</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/elements" title="把组件转换成自定义元素" target="_self"><span>Angular 元素</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/attribute-directives" title="属性型指令把行为添加到现有元素上。" target="_self"><span>属性型指令</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 expanded selected ng-star-inserted" style="position:relative" href="guide/structural-directives" title="结构型指令可以操纵页面的布局" target="_self"><span>结构型指令</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/pipes" title="管道可以在模板中转换显示的内容。" target="_self"><span>管道</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-2 collapsed ng-star-inserted" title="Angular 的表单" aria-pressed="false"><span>表单</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-2 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/forms-overview" title="表单可以创建集中、高效、引人注目的输入体验。Angular 表单可以协调一组数据绑定控件，跟踪变更，验证输入，并表达错误信息。" target="_self"><span>简介</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/reactive-forms" title="使用 FormBuilder、表单组和表单数组创建响应式表单。" target="_self"><span>响应式表单</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/forms" title="使用指令和 Angular 模板语法创建模板驱动表单。" target="_self"><span>模板驱动表单</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/form-validation" title="验证用户的表单输入" target="_self"><span>表单验证</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/dynamic-form" title="使用 FormGroup 渲染动态表单。" target="_self"><span>动态表单</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-2 collapsed ng-star-inserted" title="Observable 与 RxJS" aria-pressed="false"><span>Observable 与 RxJS</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-2 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/observables" title="" target="_self"><span>可观察对象(Observable)</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/rx-library" title="" target="_self"><span>RxJS 库</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/observables-in-angular" title="" target="_self"><span>Angular 中的可观察对象</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/practical-observable-usage" title="" target="_self"><span>用法实战</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/comparing-observables" title="" target="_self"><span>与其它技术的比较</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-2 collapsed ng-star-inserted" title="Angular 中的模块" aria-pressed="false"><span>NgModules</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-2 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/ngmodules" title="使用 NgModule 让你的应用更高效" target="_self"><span>NgModule 简介</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/ngmodule-vs-jsmodule" title="JavaScript 模块和 NgModule 之间的差异" target="_self"><span>JS 模块 vs NgModule</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/bootstrapping" title="告诉 Angular 如何在根 &quot;AppModule&quot; 中构造和引导应用。" target="_self"><span>应用的根模块</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/frequent-ngmodules" title="介绍最常用的 NgModule" target="_self"><span>常用模块</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/module-types" title="介绍特性模块的几种类型" target="_self"><span>特性模块的分类</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/entry-components" title="关于 Angular 中入口组件的一切" target="_self"><span>入口组件</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/feature-modules" title="创建特性模块，以组织你的代码" target="_self"><span>特性模块</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/providers" title="服务提供者与 NgModule" target="_self"><span>服务提供者</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/singleton-services" title="创建单例服务" target="_self"><span>单例服务</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/lazy-loading-ngmodules" title="惰性加载模块，以提高应用的性能" target="_self"><span>惰性加载的特性模块</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/sharing-ngmodules" title="共享 NgModule 让你的应用现代化。" target="_self"><span>共享 NgModule</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/ngmodule-api" title="理解 NgModule 的那些细节。" target="_self"><span>NgModule API</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/ngmodule-faq" title="回答关于 NgModules 的常见问题。" target="_self"><span>NgModule 常见问题</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-2 collapsed ng-star-inserted" title="依赖注入：创建并注入各种服务。" aria-pressed="false"><span>依赖注入</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-2 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/dependency-injection" title="Angular 的依赖注入系统能够为 Angular 创建的类创建并交付它们所依赖的服务。" target="_self"><span>Angular 依赖注入</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/hierarchical-dependency-injection" title="与组件树平行的注入器树，并支持嵌套的依赖。" target="_self"><span>多级注入器</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/dependency-injection-providers" title="各种提供者类型的更多知识。" target="_self"><span>DI 提供者</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/dependency-injection-in-action" title="依赖注入的使用技巧" target="_self"><span>DI 实战</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/dependency-injection-navtree" title="使用注入器树来查找父组件。" target="_self"><span>浏览组件树</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/http" title="通过 HTTP 协议与远程服务器对话。" target="_self"><span>HttpClient</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/router" title="揭示如何通过 Angular 路由进行基本的屏幕导航。" target="_self"><span>路由与导航</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-2 collapsed ng-star-inserted" title="Angular 动画系统指南" aria-pressed="false"><span>动画</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-2 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/animations" title="Angular 动画的基础技术。" target="_self"><span>简介</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/transition-and-triggers" title="转场与触发器的高级技术。" target="_self"><span>转场与触发器</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/complex-animation-sequences" title="复杂的 Angular 动画序列。" target="_self"><span>复杂序列</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/reusable-animations" title="创建可复用的动画。" target="_self"><span>可复用动画</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/route-animations" title="为路由提供转场动画。" target="_self"><span>路由转场动画</span></a></div></aio-nav-item></div></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-1 collapsed ng-star-inserted" title="把 Angular 用到你的实际工作中的一些技巧" aria-pressed="false"><span>其它技术</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-1 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/security" title="Angular 应用开发中的安全技术。" target="_self"><span>安全</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/i18n" title="把应用模板中的文本翻译成多种语言。" target="_self"><span>国际化（i18n）</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/accessibility" title="设计能被所有用户访问的应用" target="_self"><span>无障碍（a11y）</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-2 collapsed ng-star-inserted" title="Angular Service Worker: 控制应用资源的缓存。" aria-pressed="false"><span>Service Worker 与 PWA</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-2 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/service-worker-intro" title="Angular 对 Service Worker 的实现提升了慢速或不稳定的网络连接下的用户体验。" target="_self"><span>简介</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/service-worker-getting-started" title="在 CLI 项目中启用 Service Worker，并在浏览器中查看效果。" target="_self"><span>快速上手</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/app-shell" title="在 CLI 项目中启用应用外壳。" target="_self"><span>应用外壳</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/service-worker-communications" title="那些能让你和 Angular 的 Service Worker 通讯的服务类。" target="_self"><span>与 Service Worker 通讯</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/service-worker-devops" title="使用 Service Worker 运行应用、管理应用更新、调试以及杀掉正在运行的应用。" target="_self"><span>生产环境下的 Service Worker</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/service-worker-config" title="配置 Service Worker 的缓存行为。" target="_self"><span>Service Worker 配置</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/universal" title="使用 Angular Universal 在服务端渲染 HTML。" target="_self"><span>服务端渲染</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-2 collapsed ng-star-inserted" title="把 AngularJS 应用增量式的升级到 Angular。" aria-pressed="false"><span>从 AngularJS 升级</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-2 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/upgrade" title="把 AngularJS 应用增量式的升级到 Angular。" target="_self"><span>升级步骤</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/upgrade-performance" title="用更灵活的方式把 AngularJS 升级到 Angular。" target="_self"><span>更关注性能的升级方式</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/ajs-quick-reference" title="学习如何把 AngularJS 的概念映射到 Angular 中。" target="_self"><span>AngularJS 与 Angular 的概念对照</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-2 collapsed ng-star-inserted" title="使用共享库扩展 Angular" aria-pressed="false"><span>开发 Angular 库</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-2 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/libraries" title="理解何时以及如何使用和创建库。" target="_self"><span>库概览</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/using-libraries" title="把已发布的库集成进你的应用中。" target="_self"><span>使用已发布的库</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/creating-libraries" title="通过创建、发布和使用你自己的库来扩展 Angular。" target="_self"><span>创建库</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-2 collapsed ng-star-inserted" title="理解原理图" aria-pressed="false"><span>原理图（Schematic）</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-2 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/schematics" title="理解 Angular 如何使用原理图。" target="_self"><span>原理图概览</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/schematics-authoring" title="理解原理图的结构。" target="_self"><span>制作原理图</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/schematics-for-libraries" title="使用原理图来把你的库集成进 Angular CLI 中。" target="_self"><span>库的原理图</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/cli-builder" title="使用构建器定制 Angular CLI。" target="_self"><span>CLI 构建器</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/web-worker" title="在 Angular CLI 中使用 Web Worker。" target="_self"><span>Web Worker</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-1 collapsed ng-star-inserted" title="关于构建、测试和部署的信息" aria-pressed="false"><span>开发工作流</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-1 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-2 collapsed ng-star-inserted" title="理解 AOT 预先编译器。" aria-pressed="false"><span>AOT 预先编译器</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-2 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/aot-compiler" title="学习为何以及如何使用预先编译器。" target="_self"><span>预先编译</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/angular-compiler-options" title="配置 AOT 编译。" target="_self"><span>Angular 编译器选项</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/aot-metadata-errors" title="解决 AOT 编译错误。" target="_self"><span>AOT 元数据错误</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/template-typecheck" title="Angular 中的模板类型检查。" target="_self"><span>模板类型检查</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/build" title="构建应用及为应用启动开发服务器。" target="_self"><span>构建与运行</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/bazel" title="如何配置你的环境，以利用 Bazel 进行构建和测试。" target="_self"><span>使用 Bazel 进行构建</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/testing" title="测试 Angular 应用的技巧与实践。" target="_self"><span>测试</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/deployment" title="了解如何部署 Angular 应用。" target="_self"><span>发布</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-2 collapsed ng-star-inserted" title="与开发环境和工具集成起来" aria-pressed="false"><span>开发工具集成</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-2 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-3 collapsed ng-star-inserted" style="position:relative" href="guide/language-service" title="使用 Angular 语言服务加速开发。" target="_self"><span>语言服务</span></a></div></aio-nav-item></div></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-1 collapsed ng-star-inserted" title="工作空间与项目的结构，及其配置文件。" aria-pressed="false"><span>配置</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-1 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/file-structure" title="Angular 工作区在文件系统中是怎样的。" target="_self"><span>项目文件结构</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/workspace-config" title="&quot;angular.json&quot; 包含供 CLI 命令使用的工作区和项目默认配置。" target="_self"><span>工作区配置</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/npm-packages" title="开发期间和运行期间所需的 npm 包的说明。" target="_self"><span>npm 包</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/typescript-configuration" title="给 Angular 开发者的 TypeScript 配置。" target="_self"><span>TypeScript 配置</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/browser-support" title="浏览器支持与腻子脚本指南。" target="_self"><span>浏览器支持</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-1 collapsed ng-star-inserted" title="Angular 的版本发布实践、更新与升级。" aria-pressed="false"><span>发布信息</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-1 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/updating" title="如何把 Angular 应用和库升级到最新版本。" target="_self"><span>保持最新</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/releases" title="Angular 的版本、发布、支持、弃用策略及实践。" target="_self"><span>发布实践</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/updating-to-version-9" title="支持把你的应用从 8 升级到 9。" target="_self"><span>升级到第 9 版</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/deprecations" title="弃用的 Angular API 和特性汇总。" target="_self"><span>弃用清单</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/ivy" title="关于 Angular Ivy 的编译与渲染管道。" target="_self"><span>Angular Ivy</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-1 collapsed ng-star-inserted" title="Angular 语法、编码、术语汇总。" aria-pressed="false"><span>快捷手册</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-1 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/cheatsheet" title="关于 Angular 常用编码技术的快速指南。" target="_self"><span>速查表</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/styleguide" title="写出 Angular 风格的程序" target="_self"><span>风格指南</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="guide/glossary" title="Angular 中最重要的词汇的简要定义。" target="_self"><span>词汇表</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><button type="button" class="vertical-menu-item heading level-1 collapsed ng-star-inserted" title="Angular CLI 命令参考手册。" aria-pressed="false"><span>CLI 命令</span><mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><div class="heading-children level-1 collapsed"><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli" title="CLI 工具介绍、命令、语法" target="_self"><span>概览</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/usage-analytics-gathering" title="管理员如何从用户那里收集使用情况分析。" target="_self"><span>使用情况分析</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/add" title="ng add." target="_self"><span>ng add</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/analytics" title="ng analytics." target="_self"><span>ng analytics</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/build" title="ng build." target="_self"><span>ng build</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/config" title="ng config." target="_self"><span>ng config</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/deploy" title="ng deploy." target="_self"><span>ng deploy</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/doc" title="ng doc." target="_self"><span>ng doc</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/e2e" title="ng e2e." target="_self"><span>ng e2e</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/generate" title="ng generate." target="_self"><span>ng generate</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/help" title="ng help." target="_self"><span>ng help</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/lint" title="ng lint." target="_self"><span>ng lint</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/new" title="ng new." target="_self"><span>ng new</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/run" title="ng run." target="_self"><span>ng run</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/serve" title="ng serve." target="_self"><span>ng serve</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/test" title="ng test." target="_self"><span>ng test</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/update" title="ng update." target="_self"><span>ng update</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/version" title="ng version." target="_self"><span>ng version</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-2 collapsed ng-star-inserted" style="position:relative" href="cli/xi18n" title="ng xi18n." target="_self"><span>ng xi18n</span></a></div></aio-nav-item></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-1 collapsed ng-star-inserted" style="position:relative" href="api" title="关于 Angular 中类和值的详细信息。" target="_self"><span>API 参考手册</span></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><div class="mat-divider ng-star-inserted" style="margin:4px 20px;border-top:1px solid #d3d3d3"></div></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-1 collapsed ng-star-inserted" style="position:relative" href="https://github.com/ng-docs/ng-docs.github.io/issues" title="github 上的中文互助问答区" target="_blank"><span>互助问答</span><mat-icon role="img" class="mat-icon notranslate material-icons mat-icon-no-color ng-star-inserted" aria-hidden="true">open_in_new</mat-icon></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-1 collapsed ng-star-inserted" style="position:relative" href="https://material.angular.cn" title="Angular Material 组件库的中文文档" target="_blank"><span>官方 Material 组件库</span><mat-icon role="img" class="mat-icon notranslate material-icons mat-icon-no-color ng-star-inserted" aria-hidden="true">open_in_new</mat-icon></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-1 collapsed ng-star-inserted" style="position:relative" href="https://ng.ant.design/" title="Ant Design 的 Angular 实现，服务于企业级后台产品。" target="_blank"><span>ng-zorro 组件库</span><mat-icon role="img" class="mat-icon notranslate material-icons mat-icon-no-color ng-star-inserted" aria-hidden="true">open_in_new</mat-icon></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-1 collapsed ng-star-inserted" style="position:relative" href="https://devui.design/" title="“华为云” 开源的企业级组件库，核心设计思想是：致简、沉浸、灵活" target="_blank"><span>华为 DevUI 组件库</span><mat-icon role="img" class="mat-icon notranslate material-icons mat-icon-no-color ng-star-inserted" aria-hidden="true">open_in_new</mat-icon></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-1 collapsed ng-star-inserted" style="position:relative" href="https://ng.mobile.ant.design/" title="Ant Design Mobile 的 Angular 实现，服务于无线产品。" target="_blank"><span>ng-zorro mobile 组件库</span><mat-icon role="img" class="mat-icon notranslate material-icons mat-icon-no-color ng-star-inserted" aria-hidden="true">open_in_new</mat-icon></a></div></aio-nav-item><aio-nav-item class="ng-star-inserted"><div class="ng-star-inserted"><a class="vertical-menu-item level-1 collapsed ng-star-inserted" style="position:relative" href="https://flutter.cn/" title="Flutter 中文文档站" target="_blank"><span>友站：Flutter 中文</span><mat-icon role="img" class="mat-icon notranslate material-icons mat-icon-no-color ng-star-inserted" aria-hidden="true">open_in_new</mat-icon></a></div></aio-nav-item></aio-nav-menu><div class="doc-version ng-tns-c18-1"><aio-select><div class="form-select-menu"><button class="form-select-button"><span><strong></strong></span><span>stable (v9.0.0)</span></button></div></aio-select></div></div></mat-sidenav><div class="cdk-visually-hidden cdk-focus-trap-anchor" aria-hidden="true"></div><mat-sidenav-content cdkscrollable="" class="mat-drawer-content mat-sidenav-content ng-star-inserted" style="margin-left:261px"><main role="main" class="sidenav-content" id="guide-structural-directives"><aio-mode-banner></aio-mode-banner><aio-doc-viewer class=""><div style="opacity:1"><div class="github-links"><a href="https://github.com/angular/angular-cn/edit/aio/aio/content/guide/structural-directives.md?message=docs%3A%20请简述你的修改..." aria-label="提供编辑建议" title="提供编辑建议"><i class="material-icons" aria-hidden="true" role="img">mode_edit</i></a></div><div class="content"><h1 id="structural-directives" translation-result="on">结构型指令<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#structural-directives"><i class="material-icons">link</i></a></h1><aio-toc class="embedded" ng-version="9.0.0-rc.11"><div class="toc-inner no-print collapsed ng-star-inserted"><button type="button" title="Expand/collapse contents" aria-label="Expand/collapse contents" class="toc-heading embedded secondary ng-star-inserted" aria-pressed="false">目录<mat-icon role="img" svgicon="keyboard_arrow_right" class="mat-icon notranslate rotating-icon collapsed mat-icon-no-color" aria-hidden="true"><svg focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M8.59 16.34l4.58-4.59-4.58-4.59L10 5.75l6 6-6 6z"></path></svg></mat-icon></button><ul class="toc-list embedded"><li class="h2 ng-star-inserted" title="什么是结构型指令？"><a href="guide/structural-directives#what-are-structural-directives">什么是结构型指令？</a></li><li class="h2 ng-star-inserted" title="NgIf 案例分析"><a href="guide/structural-directives#ngif-case-study">NgIf 案例分析</a></li><li class="h3 ng-star-inserted" title="为什么移除而不是隐藏？"><a href="guide/structural-directives#why-remove-rather-than-hide">为什么<em>移除</em>而不是<em>隐藏</em>？</a></li><li class="h2 secondary ng-star-inserted" title="星号（*）前缀"><a href="guide/structural-directives#the-asterisk--prefix">星号（*）前缀</a></li><li class="h2 secondary ng-star-inserted" title="*ngFor 内幕"><a href="guide/structural-directives#inside-ngfor"><code>*ngFor</code> 内幕</a></li><li class="h2 secondary ng-star-inserted" title="微语法"><a href="guide/structural-directives#microsyntax">微语法</a></li><li class="h3 secondary ng-star-inserted" title="编写你自己的结构型指令"><a href="guide/structural-directives#writing-your-own-structural-directives">编写你自己的结构型指令</a></li><li class="h3 secondary ng-star-inserted" title="约束"><a href="guide/structural-directives#constraints">约束</a></li><li class="h3 secondary ng-star-inserted" title="语法"><a href="guide/structural-directives#grammar">语法</a></li><li class="h3 secondary ng-star-inserted" title="翻译"><a href="guide/structural-directives#translation">翻译</a></li><li class="h3 secondary ng-star-inserted" title="微语法样例"><a href="guide/structural-directives#microsyntax-examples">微语法样例</a></li><li class="h2 secondary ng-star-inserted" title="模板输入变量"><a href="guide/structural-directives#template-input-variable">模板输入变量</a></li><li class="h2 secondary ng-star-inserted" title="每个宿主元素上只能有一个结构型指令"><a href="guide/structural-directives#one-structural-directive-per-host-element">每个宿主元素上只能有一个结构型指令</a></li><li class="h2 secondary ng-star-inserted" title="NgSwitch 内幕"><a href="guide/structural-directives#inside-ngswitch-directives"><code>NgSwitch</code> 内幕</a></li><li class="h2 secondary ng-star-inserted" title="优先使用星号（*）语法"><a href="guide/structural-directives#prefer-the-asterisk--syntax">优先使用星号（<code>*</code>）语法</a></li><li class="h2 secondary ng-star-inserted" title="<ng-template>元素"><a href="guide/structural-directives#the-ng-template"><em>&lt;ng-template&gt;</em>元素</a></li><li class="h2 secondary ng-star-inserted" title="使用<ng-container>把一些兄弟元素归为一组"><a href="guide/structural-directives#group-sibling-elements-with-ng-container">使用&lt;ng-container&gt;把一些兄弟元素归为一组</a></li><li class="h3 secondary ng-star-inserted" title="<ng-container> 的救赎"><a href="guide/structural-directives#ng-container-to-the-rescue">&lt;ng-container&gt; 的救赎</a></li><li class="h2 secondary ng-star-inserted" title="写一个结构型指令"><a href="guide/structural-directives#write-a-structural-directive">写一个结构型指令</a></li><li class="h3 secondary ng-star-inserted" title="TemplateRef 和 ViewContainerRef"><a href="guide/structural-directives#templateref-and-viewcontainerref"><em>TemplateRef</em> 和 <em>ViewContainerRef</em></a></li><li class="h3 secondary ng-star-inserted" title="appUnless 属性"><a href="guide/structural-directives#the-appunless-property"><em>appUnless</em> 属性</a></li><li class="h2 secondary ng-star-inserted" title="小结"><a href="guide/structural-directives#summary">小结</a></li></ul><button type="button" title="Expand/collapse contents" aria-label="Expand/collapse contents" class="toc-more-items embedded material-icons collapsed ng-star-inserted" aria-pressed="false"></button></div></aio-toc><h1 translation-origin="off" id="structural-directives">Structural directives<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#structural-directives"><i class="material-icons">link</i></a></h1><style>h4{font-size:17px!important;text-transform:none!important}.syntax{font-family:Consolas,'Lucida Sans',Courier,sans-serif;color:#000;font-size:85%}</style><p translation-result="on">本章将看看 Angular 如何用<em>结构型指令</em>操纵 DOM 树，以及你该如何写自己的结构型指令来完成同样的任务。</p><p translation-origin="off">This guide looks at how Angular manipulates the DOM with <strong>structural directives</strong> and how you can write your own structural directives to do the same thing.</p><p translation-result="on">试试<live-example ng-version="9.0.0-rc.11"><span style="display:none"></span><span><span class="ng-star-inserted"><a target="_blank" title="现场演练" href="generated/live-examples/structural-directives/stackblitz.html">现场演练</a><span class="ng-star-inserted"> / <a download="" title="下载范例" href="generated/zips/structural-directives/structural-directives.zip">下载范例</a></span></span></span></live-example>。</p><p translation-origin="off">Try the<live-example ng-version="9.0.0-rc.11"><span style="display:none"></span><span><span class="ng-star-inserted"><a target="_blank" title="现场演练" href="generated/live-examples/structural-directives/stackblitz.html">现场演练</a><span class="ng-star-inserted"> / <a download="" title="下载范例" href="generated/zips/structural-directives/structural-directives.zip">下载范例</a></span></span></span></live-example>.</p><a id="definition"></a><h2 id="what-are-structural-directives" translation-result="on">什么是结构型指令？<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#what-are-structural-directives"><i class="material-icons">link</i></a></h2><h2 translation-origin="off" id="what-are-structural-directives">What are structural directives?<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#what-are-structural-directives"><i class="material-icons">link</i></a></h2><p translation-result="on">结构型指令的职责是 HTML 布局。 它们塑造或重塑 DOM 的结构，比如添加、移除或维护这些元素。</p><p translation-origin="off">Structural directives are responsible for HTML layout. They shape or reshape the DOM's <em>structure</em>, typically by adding, removing, or manipulating elements.</p><p translation-result="on">像其它指令一样，你可以把结构型指令应用到一个<em>宿主元素</em>上。 然后它就可以对宿主元素及其子元素做点什么。</p><p translation-origin="off">As with other directives, you apply a structural directive to a <em>host element</em>. The directive then does whatever it's supposed to do with that host element and its descendants.</p><p translation-result="on">结构型指令非常容易识别。 在这个例子中，星号（*）被放在指令的属性名之前。</p><p translation-origin="off">Structural directives are easy to recognize. An asterisk (*) precedes the directive attribute name as in this example.</p><code-example path="structural-directives/src/app/app.component.html" header="src/app/app.component.html (ngif)" region="ngif" ng-version="9.0.0-rc.11"><div style="display:none">&lt;div *<a href="api/common/NgIf" class="code-anchor">ngIf</a>="hero" class="name"&gt;{{hero.name}}&lt;/div&gt;</div><header class="ng-star-inserted">src/app/app.component.html (ngif)</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from src/app/app.component.html (ngif)">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="tag">&lt;div</span><span class="pln"> *</span><a href="api/common/NgIf" class="code-anchor"><span class="atn">ngIf</span></a><span class="pun">=</span><span class="atv">"hero"</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"name"</span><span class="tag">&gt;</span><span class="pln">{{hero.name}}</span><span class="tag">&lt;/div&gt;</span></code>
    </pre></aio-code></code-example><p translation-result="on">没有方括号，没有圆括号，只是把 <code>*<a href="api/common/NgIf" class="code-anchor">ngIf</a></code> 设置为一个字符串。</p><p translation-origin="off">No brackets. No parentheses. Just <code>*<a href="api/common/NgIf" class="code-anchor">ngIf</a></code> set to a string.</p><p translation-result="on">在这个例子中，你将学到<a href="guide/structural-directives#asterisk">星号(*)这个简写方法</a>，而这个字符串是一个<a href="guide/structural-directives#microsyntax"><em>微语法</em></a>，而不是通常的<a href="guide/template-syntax#template-expressions">模板表达式</a>。 Angular 会解开这个语法糖，变成一个 <code>&lt;ng-template&gt;</code> 标记，包裹着宿主元素及其子元素。 每个结构型指令都可以用这个模板做点不同的事情。</p><p translation-origin="off">You'll learn in this guide that the <a href="guide/structural-directives#asterisk">asterisk (*) is a convenience notation</a> and the string is a <a href="guide/structural-directives#microsyntax"><em>microsyntax</em></a> rather than the usual <a href="guide/template-syntax#template-expressions">template expression</a>. Angular desugars this notation into a marked-up <code>&lt;ng-template&gt;</code> that surrounds the host element and its descendents. Each structural directive does something different with that template.</p><p translation-result="on">三个常用的内置结构型指令 —— <a href="guide/template-syntax#ngIf">NgIf</a>、<a href="guide/template-syntax#ngFor">NgFor</a>和<a href="guide/template-syntax#ngSwitch">NgSwitch...</a>。 你在<a href="guide/template-syntax"><em>模板语法</em></a>一章中学过它，并且在 Angular 文档的例子中到处都在用它。下面是模板中的例子：</p><p translation-origin="off">Three of the common, built-in structural directives—<a href="guide/template-syntax#ngIf">NgIf</a>, <a href="guide/template-syntax#ngFor">NgFor</a>, and <a href="guide/template-syntax#ngSwitch">NgSwitch...</a>—are described in the <a href="guide/template-syntax"><em>Template Syntax</em></a> guide and seen in samples throughout the Angular documentation. Here's an example of them in a template:</p><code-example path="structural-directives/src/app/app.component.html" header="src/app/app.component.html (built-in)" region="built-in" ng-version="9.0.0-rc.11"><div style="display:none">&lt;div *<a href="api/common/NgIf" class="code-anchor">ngIf</a>="hero" class="name"&gt;{{hero.name}}&lt;/div&gt; &lt;ul&gt; &lt;li *<a href="api/common/NgForOf" class="code-anchor">ngFor</a>="let hero of heroes"&gt;{{hero.name}}&lt;/li&gt; &lt;/ul&gt; &lt;div [<a href="api/common/NgSwitch" class="code-anchor">ngSwitch</a>]="hero?.emotion"&gt; &lt;app-happy-hero *<a href="api/common/NgSwitchCase" class="code-anchor">ngSwitchCase</a>="'happy'" [hero]="hero"&gt;&lt;/app-happy-hero&gt; &lt;app-sad-hero *<a href="api/common/NgSwitchCase" class="code-anchor">ngSwitchCase</a>="'sad'" [hero]="hero"&gt;&lt;/app-sad-hero&gt; &lt;app-confused-hero *<a href="api/common/NgSwitchCase" class="code-anchor">ngSwitchCase</a>="'confused'" [hero]="hero"&gt;&lt;/app-confused-hero&gt; &lt;app-unknown-hero *<a href="api/common/NgSwitchDefault" class="code-anchor">ngSwitchDefault</a> [hero]="hero"&gt;&lt;/app-unknown-hero&gt; &lt;/div&gt;</div><header class="ng-star-inserted">src/app/app.component.html (built-in)</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from src/app/app.component.html (built-in)">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="tag">&lt;div</span><span class="pln"> *</span><a href="api/common/NgIf" class="code-anchor"><span class="atn">ngIf</span></a><span class="pun">=</span><span class="atv">"hero"</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"name"</span><span class="tag">&gt;</span><span class="pln">{{hero.name}}</span><span class="tag">&lt;/div&gt;</span><span class="pln">

</span><span class="tag">&lt;ul&gt;</span><span class="pln">
  </span><span class="tag">&lt;li</span><span class="pln"> *</span><a href="api/common/NgForOf" class="code-anchor"><span class="atn">ngFor</span></a><span class="pun">=</span><span class="atv">"let hero of heroes"</span><span class="tag">&gt;</span><span class="pln">{{hero.name}}</span><span class="tag">&lt;/li&gt;</span><span class="pln">
</span><span class="tag">&lt;/ul&gt;</span><span class="pln">

</span><span class="tag">&lt;div</span><span class="pln"> [</span><a href="api/common/NgSwitch" class="code-anchor"><span class="atn">ngSwitch</span></a><span class="pln">]</span><span class="pun">=</span><span class="atv">"hero?.emotion"</span><span class="tag">&gt;</span><span class="pln">
  </span><span class="tag">&lt;app-happy-hero</span><span class="pln">    *</span><a href="api/common/NgSwitchCase" class="code-anchor"><span class="atn">ngSwitchCase</span></a><span class="pun">=</span><span class="atv">"'happy'"</span><span class="pln">    [</span><span class="atn">hero</span><span class="pln">]</span><span class="pun">=</span><span class="atv">"hero"</span><span class="tag">&gt;&lt;/app-happy-hero&gt;</span><span class="pln">
  </span><span class="tag">&lt;app-sad-hero</span><span class="pln">      *</span><a href="api/common/NgSwitchCase" class="code-anchor"><span class="atn">ngSwitchCase</span></a><span class="pun">=</span><span class="atv">"'sad'"</span><span class="pln">      [</span><span class="atn">hero</span><span class="pln">]</span><span class="pun">=</span><span class="atv">"hero"</span><span class="tag">&gt;&lt;/app-sad-hero&gt;</span><span class="pln">
  </span><span class="tag">&lt;app-confused-hero</span><span class="pln"> *</span><a href="api/common/NgSwitchCase" class="code-anchor"><span class="atn">ngSwitchCase</span></a><span class="pun">=</span><span class="atv">"'confused'"</span><span class="pln"> [</span><span class="atn">hero</span><span class="pln">]</span><span class="pun">=</span><span class="atv">"hero"</span><span class="tag">&gt;&lt;/app-confused-hero&gt;</span><span class="pln">
  </span><span class="tag">&lt;app-unknown-hero</span><span class="pln">  *</span><a href="api/common/NgSwitchDefault" class="code-anchor"><span class="atn">ngSwitchDefault</span></a><span class="pln">           [</span><span class="atn">hero</span><span class="pln">]</span><span class="pun">=</span><span class="atv">"hero"</span><span class="tag">&gt;&lt;/app-unknown-hero&gt;</span><span class="pln">
</span><span class="tag">&lt;/div&gt;</span></code>
    </pre></aio-code></code-example><p translation-result="on">本章不会重复讲如何<em>使用</em>它们，而是解释它们的<em>工作原理</em>以及如何<a href="guide/structural-directives#unless">写自己的结构型指令</a>。</p><p translation-origin="off">This guide won't repeat how to <em>use</em> them. But it does explain <em>how they work</em> and how to <a href="guide/structural-directives#unless">write your own</a> structural directive.</p><div class="callout is-helpful"><header translation-result="on">指令的拼写形式</header><header translation-origin="off">Directive spelling</header><p translation-result="on">在本章中，你将看到指令同时具有两种拼写形式<em>大驼峰 <code>UpperCamelCase</code> 和小驼峰 <code>lowerCamelCase</code>，比如你已经看过的 <code><a href="api/common/NgIf" class="code-anchor">NgIf</a></code> 和 <code><a href="api/common/NgIf" class="code-anchor">ngIf</a></code>。 这里的原因在于，<code><a href="api/common/NgIf" class="code-anchor">NgIf</a></code> 引用的是指令的</em>类名<em>，而 <code><a href="api/common/NgIf" class="code-anchor">ngIf</a></code> 引用的是指令的</em>属性名*。</p><p translation-origin="off">Throughout this guide, you'll see a directive spelled in both <em>UpperCamelCase</em> and <em>lowerCamelCase</em>. Already you've seen <code><a href="api/common/NgIf" class="code-anchor">NgIf</a></code> and <code><a href="api/common/NgIf" class="code-anchor">ngIf</a></code>. There's a reason. <code><a href="api/common/NgIf" class="code-anchor">NgIf</a></code> refers to the directive <em>class</em>; <code><a href="api/common/NgIf" class="code-anchor">ngIf</a></code> refers to the directive's <em>attribute name</em>.</p><p translation-result="on">指令的<em>类名</em>拼写成<em>大驼峰形式</em>（<code><a href="api/common/NgIf" class="code-anchor">NgIf</a></code>），而它的<em>属性名</em>则拼写成<em>小驼峰形式</em>（<code><a href="api/common/NgIf" class="code-anchor">ngIf</a></code>）。 本章会在谈论指令的属性和工作原理时引用指令的<em>类名</em>，在描述如何在 HTML 模板中把该指令应用到元素时，引用指令的<em>属性名</em>。</p><p translation-origin="off">A directive <em>class</em> is spelled in <em>UpperCamelCase</em> (<code><a href="api/common/NgIf" class="code-anchor">NgIf</a></code>). A directive's <em>attribute name</em> is spelled in <em>lowerCamelCase</em> (<code><a href="api/common/NgIf" class="code-anchor">ngIf</a></code>). The guide refers to the directive <em>class</em> when talking about its properties and what the directive does. The guide refers to the <em>attribute name</em> when describing how you apply the directive to an element in the HTML template.</p></div><div class="alert is-helpful"><p translation-result="on">还有另外两种 Angular 指令，在本开发指南的其它地方有讲解：(1) 组件 (2) 属性型指令。</p><p translation-origin="off">There are two other kinds of Angular directives, described extensively elsewhere: (1)&nbsp;components and (2)&nbsp;attribute directives.</p><p translation-result="on"><em>组件</em>可以在原生 HTML 元素中管理一小片区域的 HTML。从技术角度说，它就是一个带模板的指令。</p><p translation-origin="off">A <em>component</em> manages a region of HTML in the manner of a native HTML element. Technically it's a directive with a template.</p><p translation-result="on"><a href="guide/attribute-directives"><em>属性型</em>指令</a>会改变某个元素、组件或其它指令的外观或行为。 比如，内置的<a href="guide/template-syntax#ngStyle"><code>NgStyle</code></a>指令可以同时修改元素的多个样式。</p><p translation-origin="off">An <a href="guide/attribute-directives"><em>attribute</em> directive</a> changes the appearance or behavior of an element, component, or another directive. For example, the built-in <a href="guide/template-syntax#ngStyle"><code>NgStyle</code></a>directive changes several element styles at the same time.</p><p translation-result="on">你可以在一个宿主元素上应用多个<em>属性型</em>指令，但<a href="guide/structural-directives#one-per-element">只能应用一个</a><em>结构型</em>指令。</p><p translation-origin="off">You can apply many <em>attribute</em> directives to one host element. You can <a href="guide/structural-directives#one-per-element">only apply one</a> <em>structural</em> directive to a host element.</p></div><a id="ngIf"></a><h2 id="ngif-case-study" translation-result="on">NgIf 案例分析<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#ngif-case-study"><i class="material-icons">link</i></a></h2><h2 translation-origin="off" id="ngif-case-study">NgIf case study<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#ngif-case-study"><i class="material-icons">link</i></a></h2><p translation-result="on"><code><a href="api/common/NgIf" class="code-anchor">NgIf</a></code> 是一个很好的结构型指令案例：它接受一个布尔值，并据此让一整块 DOM 树出现或消失。</p><p translation-origin="off"><code><a href="api/common/NgIf" class="code-anchor">NgIf</a></code> is the simplest structural directive and the easiest to understand. It takes a boolean expression and makes an entire chunk of the DOM appear or disappear.</p><code-example path="structural-directives/src/app/app.component.html" header="src/app/app.component.html (ngif-true)" region="ngif-true" ng-version="9.0.0-rc.11"><div style="display:none">&lt;p *<a href="api/common/NgIf" class="code-anchor">ngIf</a>="true"&gt; Expression is true and <a href="api/common/NgIf" class="code-anchor">ngIf</a> is true. This paragraph is in the DOM. &lt;/p&gt; &lt;p *<a href="api/common/NgIf" class="code-anchor">ngIf</a>="false"&gt; Expression is false and <a href="api/common/NgIf" class="code-anchor">ngIf</a> is false. This paragraph is not in the DOM. &lt;/p&gt;</div><header class="ng-star-inserted">src/app/app.component.html (ngif-true)</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from src/app/app.component.html (ngif-true)">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="tag">&lt;p</span><span class="pln"> *</span><a href="api/common/NgIf" class="code-anchor"><span class="atn">ngIf</span></a><span class="pun">=</span><span class="atv">"true"</span><span class="tag">&gt;</span><span class="pln">
  Expression is true and </span><a href="api/common/NgIf" class="code-anchor"><span class="pln">ngIf</span></a><span class="pln"> is true.
  This paragraph is in the DOM.
</span><span class="tag">&lt;/p&gt;</span><span class="pln">
</span><span class="tag">&lt;p</span><span class="pln"> *</span><a href="api/common/NgIf" class="code-anchor"><span class="atn">ngIf</span></a><span class="pun">=</span><span class="atv">"false"</span><span class="tag">&gt;</span><span class="pln">
  Expression is false and </span><a href="api/common/NgIf" class="code-anchor"><span class="pln">ngIf</span></a><span class="pln"> is false.
  This paragraph is not in the DOM.
</span><span class="tag">&lt;/p&gt;</span></code>
    </pre></aio-code></code-example><p translation-result="on"><code><a href="api/common/NgIf" class="code-anchor">ngIf</a></code> 指令并不是使用 CSS 来隐藏元素的。它会把这些元素从 DOM 中物理删除。 使用浏览器的开发者工具就可以确认这一点。</p><p translation-origin="off">The <code><a href="api/common/NgIf" class="code-anchor">ngIf</a></code> directive doesn't hide elements with CSS. It adds and removes them physically from the DOM. Confirm that fact using browser developer tools to inspect the DOM.</p><div class="lightbox"><img src="generated/images/guide/structural-directives/element-not-in-dom.png" alt="ngIf=false element not in DOM" width="322" height="114"></div><p translation-result="on">可以看到第一段文字出现在了 DOM 中，而第二段则没有，在第二段的位置上是一个关于“绑定”的注释（<a href="guide/structural-directives#asterisk">稍后</a>有更多讲解）。</p><p translation-origin="off">The top paragraph is in the DOM. The bottom, disused paragraph is not; in its place is a comment about "bindings" (more about that <a href="guide/structural-directives#asterisk">later</a>).</p><p translation-result="on">当条件为假时，<code><a href="api/common/NgIf" class="code-anchor">NgIf</a></code> 会从 DOM 中移除它的宿主元素，取消它监听过的那些 DOM 事件，从 Angular 变更检测中移除该组件，并销毁它。 这些组件和 DOM 节点可以被当做垃圾收集起来，并且释放它们占用的内存。</p><p translation-origin="off">When the condition is false, <code><a href="api/common/NgIf" class="code-anchor">NgIf</a></code> removes its host element from the DOM, detaches it from DOM events (the attachments that it made), detaches the component from Angular change detection, and destroys it. The component and DOM nodes can be garbage-collected and free up memory.</p><h3 id="why-remove-rather-than-hide" translation-result="on">为什么<em>移除</em>而不是<em>隐藏</em>？<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#why-remove-rather-than-hide"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="why-remove-rather-than-hide">Why <em>remove</em> rather than <em>hide</em>?<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#why-remove-rather-than-hide"><i class="material-icons">link</i></a></h3><p translation-result="on">指令也可以通过把它的 <code>display</code> 风格设置为 <code>none</code> 而隐藏不需要的段落。</p><p translation-origin="off">A directive could hide the unwanted paragraph instead by setting its <code>display</code> style to <code>none</code>.</p><code-example path="structural-directives/src/app/app.component.html" header="src/app/app.component.html (display-none)" region="display-none" ng-version="9.0.0-rc.11"><div style="display:none">&lt;p [style.display]="'block'"&gt; Expression sets display to "block". This paragraph is visible. &lt;/p&gt; &lt;p [style.display]="'none'"&gt; Expression sets display to "none". This paragraph is hidden but still in the DOM. &lt;/p&gt;</div><header class="ng-star-inserted">src/app/app.component.html (display-none)</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from src/app/app.component.html (display-none)">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="tag">&lt;p</span><span class="pln"> [</span><span class="atn">style</span><span class="pln">.</span><span class="atn">display</span><span class="pln">]</span><span class="pun">=</span><span class="atv">"'block'"</span><span class="tag">&gt;</span><span class="pln">
  Expression sets display to "block".
  This paragraph is visible.
</span><span class="tag">&lt;/p&gt;</span><span class="pln">
</span><span class="tag">&lt;p</span><span class="pln"> [</span><span class="atn">style</span><span class="pln">.</span><span class="atn">display</span><span class="pln">]</span><span class="pun">=</span><span class="atv">"'none'"</span><span class="tag">&gt;</span><span class="pln">
  Expression sets display to "none".
  This paragraph is hidden but still in the DOM.
</span><span class="tag">&lt;/p&gt;</span></code>
    </pre></aio-code></code-example><p translation-result="on">当不可见时，这个元素仍然留在 DOM 中。</p><p translation-origin="off">While invisible, the element remains in the DOM.</p><div class="lightbox"><img src="generated/images/guide/structural-directives/element-display-in-dom.png" alt="hidden element still in DOM" width="415" height="185"></div><p translation-result="on">对于简单的段落，隐藏和移除之间的差异影响不大，但对于资源占用较多的组件是不一样的。 当隐藏掉一个元素时，组件的行为还在继续 —— 它仍然附加在它所属的 DOM 元素上， 它也仍在监听事件。Angular 会继续检查哪些能影响数据绑定的变更。 组件原本要做的那些事情仍在继续。</p><p translation-origin="off">The difference between hiding and removing doesn't matter for a simple paragraph. It does matter when the host element is attached to a resource intensive component. Such a component's behavior continues even when hidden. The component stays attached to its DOM element. It keeps listening to events. Angular keeps checking for changes that could affect data bindings. Whatever the component was doing, it keeps doing.</p><p translation-result="on">虽然不可见，组件及其各级子组件仍然占用着资源，而这些资源如果分配给别人可能会更有用。 在性能和内存方面的负担相当可观，响应度会降低，而用户却可能无法从中受益。</p><p translation-origin="off">Although invisible, the component—and all of its descendant components—tie up resources. The performance and memory burden can be substantial, responsiveness can degrade, and the user sees nothing.</p><p translation-result="on">当然，从积极的一面看，重新显示这个元素会非常快。 组件以前的状态被保留着，并随时可以显示。 组件不用重新初始化 —— 该操作可能会比较昂贵。 这时候隐藏和显示就成了正确的选择。</p><p translation-origin="off">On the positive side, showing the element again is quick. The component's previous state is preserved and ready to display. The component doesn't re-initialize—an operation that could be expensive. So hiding and showing is sometimes the right thing to do.</p><p translation-result="on">但是，除非有非常强烈的理由来保留它们，否则你会更倾向于移除用户看不见的那些 DOM 元素，并且使用 <code><a href="api/common/NgIf" class="code-anchor">NgIf</a></code> 这样的结构型指令来收回用不到的资源。</p><p translation-origin="off">But in the absence of a compelling reason to keep them around, your preference should be to remove DOM elements that the user can't see and recover the unused resources with a structural directive like <code><a href="api/common/NgIf" class="code-anchor">NgIf</a></code> .</p><p translation-result="on"><strong>同样的考量也适用于每一个结构型指令，无论是内置的还是自定义的。</strong> 你应该提醒自己慎重考虑添加元素、移除元素以及创建和销毁组件的后果。</p><p translation-origin="off"><strong>These same considerations apply to every structural directive, whether built-in or custom.</strong> Before applying a structural directive, you might want to pause for a moment to consider the consequences of adding and removing elements and of creating and destroying components.</p><a id="asterisk"></a><h2 id="the-asterisk--prefix" translation-result="on">星号（*）前缀<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#the-asterisk--prefix"><i class="material-icons">link</i></a></h2><h2 translation-origin="off" id="the-asterisk--prefix">The asterisk (*) prefix<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#the-asterisk--prefix"><i class="material-icons">link</i></a></h2><p translation-result="on">你可能注意到了指令名的星号（*）前缀，并且困惑于为什么需要它以及它是做什么的。</p><p translation-origin="off">Surely you noticed the asterisk (*) prefix to the directive name and wondered why it is necessary and what it does.</p><p translation-result="on">这里的 <code>*<a href="api/common/NgIf" class="code-anchor">ngIf</a></code> 会在 <code>hero</code> 存在时显示英雄的名字。</p><p translation-origin="off">Here is <code>*<a href="api/common/NgIf" class="code-anchor">ngIf</a></code> displaying the hero's name if <code>hero</code> exists.</p><code-example path="structural-directives/src/app/app.component.html" header="src/app/app.component.html (asterisk)" region="asterisk" ng-version="9.0.0-rc.11"><div style="display:none">&lt;div *<a href="api/common/NgIf" class="code-anchor">ngIf</a>="hero" class="name"&gt;{{hero.name}}&lt;/div&gt;</div><header class="ng-star-inserted">src/app/app.component.html (asterisk)</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from src/app/app.component.html (asterisk)">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="tag">&lt;div</span><span class="pln"> *</span><a href="api/common/NgIf" class="code-anchor"><span class="atn">ngIf</span></a><span class="pun">=</span><span class="atv">"hero"</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"name"</span><span class="tag">&gt;</span><span class="pln">{{hero.name}}</span><span class="tag">&lt;/div&gt;</span></code>
    </pre></aio-code></code-example><p translation-result="on">星号是一个用来简化更复杂语法的“语法糖”。 从内部实现来说，Angular 把 <code>*<a href="api/common/NgIf" class="code-anchor">ngIf</a></code> <em>属性</em> 翻译成一个 <code>&lt;ng-template&gt;</code> <em>元素</em> 并用它来包裹宿主元素，代码如下：</p><p translation-origin="off">The asterisk is "syntactic sugar" for something a bit more complicated. Internally, Angular translates the <code>*<a href="api/common/NgIf" class="code-anchor">ngIf</a></code> <em>attribute</em> into a <code>&lt;ng-template&gt;</code> <em>element</em>, wrapped around the host element, like this.</p><code-example path="structural-directives/src/app/app.component.html" header="src/app/app.component.html (ngif-template)" region="ngif-template" ng-version="9.0.0-rc.11"><div style="display:none">&lt;ng-template [<a href="api/common/NgIf" class="code-anchor">ngIf</a>]="hero"&gt; &lt;div class="name"&gt;{{hero.name}}&lt;/div&gt; &lt;/ng-template&gt;</div><header class="ng-star-inserted">src/app/app.component.html (ngif-template)</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from src/app/app.component.html (ngif-template)">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="tag">&lt;ng-template</span><span class="pln"> [</span><a href="api/common/NgIf" class="code-anchor"><span class="atn">ngIf</span></a><span class="pln">]</span><span class="pun">=</span><span class="atv">"hero"</span><span class="tag">&gt;</span><span class="pln">
  </span><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"name"</span><span class="tag">&gt;</span><span class="pln">{{hero.name}}</span><span class="tag">&lt;/div&gt;</span><span class="pln">
</span><span class="tag">&lt;/ng-template&gt;</span></code>
    </pre></aio-code></code-example><ul><li><p translation-result="on"> <code>*<a href="api/common/NgIf" class="code-anchor">ngIf</a></code> 指令被移到了 <code>&lt;ng-template&gt;</code> 元素上。在那里它变成了一个属性绑定 <code>[<a href="api/common/NgIf" class="code-anchor">ngIf</a>]</code>。</p><p translation-origin="off">The <code>*<a href="api/common/NgIf" class="code-anchor">ngIf</a></code> directive moved to the <code>&lt;ng-template&gt;</code> element where it became a property binding,<code>[<a href="api/common/NgIf" class="code-anchor">ngIf</a>]</code>.</p></li><li><p translation-result="on"> <code>&lt;div&gt;</code> 上的其余部分，包括它的 <code>class</code> 属性在内，移到了内部的 <code>&lt;ng-template&gt;</code> 元素上。</p><p translation-origin="off">The rest of the <code>&lt;div&gt;</code>, including its class attribute, moved inside the <code>&lt;ng-template&gt;</code> element.</p></li></ul><p translation-result="on">第一种形态永远不会真的渲染出来。 只有最终产出的结果才会出现在 DOM 中。</p><p translation-origin="off">The first form is not actually rendered, only the finished product ends up in the DOM.</p><div class="lightbox"><img src="generated/images/guide/structural-directives/hero-div-in-dom.png" alt="hero div in DOM" width="330" height="67"></div><p translation-result="on">Angular 会在真正渲染的时候填充 <code>&lt;ng-template&gt;</code> 的内容，并且把 <code>&lt;ng-template&gt;</code> 替换为一个供诊断用的注释。</p><p translation-origin="off">Angular consumed the <code>&lt;ng-template&gt;</code> content during its actual rendering and replaced the <code>&lt;ng-template&gt;</code> with a diagnostic comment.</p><p translation-result="on"><a href="guide/structural-directives#ngFor"><code>NgFor</code></a>和<a href="guide/structural-directives#ngSwitch"><code>NgSwitch...</code></a>指令也都遵循同样的模式。</p><p translation-origin="off">The <a href="guide/structural-directives#ngFor"><code>NgFor</code></a>and <a href="guide/structural-directives#ngSwitch"><code>NgSwitch...</code></a>directives follow the same pattern.</p><a id="ngFor"></a><h2 id="inside-ngfor" translation-result="on"><code>*<a href="api/common/NgForOf" class="code-anchor">ngFor</a></code> 内幕<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#inside-ngfor"><i class="material-icons">link</i></a></h2><h2 translation-origin="off" id="inside-ngfor">Inside <em>*ngFor</em><a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#inside-ngfor"><i class="material-icons">link</i></a></h2><p translation-result="on">Angular 会把 <code>*<a href="api/common/NgForOf" class="code-anchor">ngFor</a></code> 用同样的方式把星号（<code>*</code>）语法的 <code>template</code><em>属性</em>转换成 <code>&lt;ng-template&gt;</code><em>元素</em>。</p><p translation-origin="off">Angular transforms the <code>*<a href="api/common/NgForOf" class="code-anchor">ngFor</a></code> in similar fashion from asterisk (*) syntax to <code>&lt;ng-template&gt;</code> <em>element</em>.</p><p translation-result="on">这里有一个 <code>NgFor</code> 的全特性应用，同时用了这两种写法：</p><p translation-origin="off">Here's a full-featured application of <code>NgFor</code>, written both ways:</p><code-example path="structural-directives/src/app/app.component.html" header="src/app/app.component.html (inside-ngfor)" region="inside-ngfor" ng-version="9.0.0-rc.11"><div style="display:none">&lt;div *<a href="api/common/NgForOf" class="code-anchor">ngFor</a>="let hero of heroes; let i=index; let <a href="api/common/NgForOfContext#odd" class="code-anchor">odd</a>=<a href="api/common/NgForOfContext#odd" class="code-anchor">odd</a>; trackBy: <a href="api/core/IterableChangeRecord#trackById" class="code-anchor">trackById</a>" [class.odd]="<a href="api/common/NgForOfContext#odd" class="code-anchor">odd</a>"&gt; ({{i}}) {{hero.name}} &lt;/div&gt; &lt;ng-template <a href="api/common/NgForOf" class="code-anchor">ngFor</a> let-hero [<a href="api/common/NgForOf" class="code-anchor">ngForOf</a>]="heroes" let-i="index" let-odd="<a href="api/common/NgForOfContext#odd" class="code-anchor">odd</a>" [<a href="api/common/NgForOf#ngForTrackBy" class="code-anchor">ngForTrackBy</a>]="<a href="api/core/IterableChangeRecord#trackById" class="code-anchor">trackById</a>"&gt; &lt;div [class.odd]="<a href="api/common/NgForOfContext#odd" class="code-anchor">odd</a>"&gt;({{i}}) {{hero.name}}&lt;/div&gt; &lt;/ng-template&gt;</div><header class="ng-star-inserted">src/app/app.component.html (inside-ngfor)</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from src/app/app.component.html (inside-ngfor)">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="tag">&lt;div</span><span class="pln"> *</span><a href="api/common/NgForOf" class="code-anchor"><span class="atn">ngFor</span></a><span class="pun">=</span><span class="atv">"let hero of heroes; let i=index; let </span><a href="api/common/NgForOfContext#odd" class="code-anchor"><span class="atv">odd</span></a><span class="atv">=</span><a href="api/common/NgForOfContext#odd" class="code-anchor"><span class="atv">odd</span></a><span class="atv">; trackBy: </span><a href="api/core/IterableChangeRecord#trackById" class="code-anchor"><span class="atv">trackById</span></a><span class="atv">"</span><span class="pln"> [</span><span class="atn">class</span><span class="pln">.</span><span class="atn">odd</span><span class="pln">]</span><span class="pun">=</span><span class="atv">"</span><a href="api/common/NgForOfContext#odd" class="code-anchor"><span class="atv">odd</span></a><span class="atv">"</span><span class="tag">&gt;</span><span class="pln">
  ({{i}}) {{hero.name}}
</span><span class="tag">&lt;/div&gt;</span><span class="pln">

</span><span class="tag">&lt;ng-template</span><span class="pln"> </span><a href="api/common/NgForOf" class="code-anchor"><span class="atn">ngFor</span></a><span class="pln"> </span><span class="atn">let-hero</span><span class="pln"> [</span><a href="api/common/NgForOf" class="code-anchor"><span class="atn">ngForOf</span></a><span class="pln">]</span><span class="pun">=</span><span class="atv">"heroes"</span><span class="pln"> </span><span class="atn">let-i</span><span class="pun">=</span><span class="atv">"index"</span><span class="pln"> </span><span class="atn">let-odd</span><span class="pun">=</span><span class="atv">"</span><a href="api/common/NgForOfContext#odd" class="code-anchor"><span class="atv">odd</span></a><span class="atv">"</span><span class="pln"> [</span><a href="api/common/NgForOf#ngForTrackBy" class="code-anchor"><span class="atn">ngForTrackBy</span></a><span class="pln">]</span><span class="pun">=</span><span class="atv">"</span><a href="api/core/IterableChangeRecord#trackById" class="code-anchor"><span class="atv">trackById</span></a><span class="atv">"</span><span class="tag">&gt;</span><span class="pln">
  </span><span class="tag">&lt;div</span><span class="pln"> [</span><span class="atn">class</span><span class="pln">.</span><span class="atn">odd</span><span class="pln">]</span><span class="pun">=</span><span class="atv">"</span><a href="api/common/NgForOfContext#odd" class="code-anchor"><span class="atv">odd</span></a><span class="atv">"</span><span class="tag">&gt;</span><span class="pln">({{i}}) {{hero.name}}</span><span class="tag">&lt;/div&gt;</span><span class="pln">
</span><span class="tag">&lt;/ng-template&gt;</span></code>
    </pre></aio-code></code-example><p translation-result="on">它明显比 <code><a href="api/common/NgIf" class="code-anchor">ngIf</a></code> 复杂得多，确实如此。 <code>NgFor</code> 指令比本章展示过的 <code><a href="api/common/NgIf" class="code-anchor">NgIf</a></code> 具有更多的必选特性和可选特性。 至少 <code>NgFor</code> 会需要一个循环变量（<code>let hero</code>）和一个列表（<code>heroes</code>）。</p><p translation-origin="off">This is manifestly more complicated than <code><a href="api/common/NgIf" class="code-anchor">ngIf</a></code> and rightly so. The <code>NgFor</code> directive has more features, both required and optional, than the <code><a href="api/common/NgIf" class="code-anchor">NgIf</a></code> shown in this guide. At minimum <code>NgFor</code> needs a looping variable (<code>let hero</code>) and a list (<code>heroes</code>).</p><p translation-result="on">你可以通过把一个字符串赋值给 <code><a href="api/common/NgForOf" class="code-anchor">ngFor</a></code> 来启用这些特性，这个字符串使用 Angular 的<a href="guide/structural-directives#microsyntax">微语法</a>。</p><p translation-origin="off">You enable these features in the string assigned to <code><a href="api/common/NgForOf" class="code-anchor">ngFor</a></code>, which you write in Angular's <a href="guide/structural-directives#microsyntax">microsyntax</a>.</p><div class="alert is-helpful"><p translation-result="on"><code><a href="api/common/NgForOf" class="code-anchor">ngFor</a></code> 字符串<em>之外</em>的每一样东西都会留在宿主元素（<code>&lt;div&gt;</code>）上，也就是说它移到了 <code>&lt;ng-template&gt;</code> 内部。 在这个例子中，<code>[<a href="api/common/NgClass" class="code-anchor">ngClass</a>]="<a href="api/common/NgForOfContext#odd" class="code-anchor">odd</a>"</code> 留在了 <code>&lt;div&gt;</code> 上。</p><p translation-origin="off">Everything <em>outside</em> the <code><a href="api/common/NgForOf" class="code-anchor">ngFor</a></code> string stays with the host element (the <code>&lt;div&gt;</code>) as it moves inside the <code>&lt;ng-template&gt;</code>. In this example, the <code>[<a href="api/common/NgClass" class="code-anchor">ngClass</a>]="<a href="api/common/NgForOfContext#odd" class="code-anchor">odd</a>"</code> stays on the <code>&lt;div&gt;</code>.</p></div><a id="microsyntax"></a><h2 id="microsyntax" translation-result="on">微语法<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#microsyntax"><i class="material-icons">link</i></a></h2><h2 translation-origin="off" id="microsyntax">Microsyntax<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#microsyntax"><i class="material-icons">link</i></a></h2><p translation-result="on">Angular 微语法能让你通过简短的、友好的字符串来配置一个指令。 微语法解析器把这个字符串翻译成 <code>&lt;ng-template&gt;</code> 上的属性：</p><p translation-origin="off">The Angular microsyntax lets you configure a directive in a compact, friendly string. The microsyntax parser translates that string into attributes on the <code>&lt;ng-template&gt;</code>:</p><ul><li><p translation-result="on"> <code>let</code> 关键字声明一个<a href="guide/structural-directives#template-input-variable">模板输入变量</a>，你会在模板中引用它。本例子中，这个输入变量就是 <code>hero</code>、<code>i</code> 和 <code><a href="api/common/NgForOfContext#odd" class="code-anchor">odd</a></code>。 解析器会把 <code>let hero</code>、<code>let i</code> 和 <code>let <a href="api/common/NgForOfContext#odd" class="code-anchor">odd</a></code> 翻译成命名变量 <code>let-hero</code>、<code>let-i</code> 和 <code>let-odd</code>。</p><p translation-origin="off">The <code>let</code> keyword declares a <a href="guide/structural-directives#template-input-variable"><em>template input variable</em></a> that you reference within the template. The input variables in this example are <code>hero</code>, <code>i</code>, and <code><a href="api/common/NgForOfContext#odd" class="code-anchor">odd</a></code>. The parser translates <code>let hero</code>, <code>let i</code>, and <code>let <a href="api/common/NgForOfContext#odd" class="code-anchor">odd</a></code> into variables named <code>let-hero</code>, <code>let-i</code>, and <code>let-odd</code>.</p></li><li><p translation-result="on">微语法解析器接收 <code>of</code> 和 <code>trackby</code>，把它们首字母大写（<code>of</code> -&gt; <code>Of</code>, <code>trackBy</code> -&gt; <code>TrackBy</code>）， 并且给它们加上指令的属性名（<code><a href="api/common/NgForOf" class="code-anchor">ngFor</a></code>）前缀，最终生成的名字是 <code><a href="api/common/NgForOf" class="code-anchor">ngForOf</a></code> 和 <code><a href="api/common/NgForOf#ngForTrackBy" class="code-anchor">ngForTrackBy</a></code>。 这两个最终生成的名字是 <code>NgFor</code> 的<em>输入属性</em>，指令据此了解到列表是 <code>heroes</code>，而 track-by 函数是 <code><a href="api/core/IterableChangeRecord#trackById" class="code-anchor">trackById</a></code>。</p><p translation-origin="off">The microsyntax parser title-cases all directives and prefixes them with the directive's attribute name, such as <code><a href="api/common/NgForOf" class="code-anchor">ngFor</a></code>. For example, the <code><a href="api/common/NgForOf" class="code-anchor">ngFor</a></code> input properties, <code>of</code> and <code>trackBy</code>, become <code><a href="api/common/NgForOf" class="code-anchor">ngForOf</a></code> and <code><a href="api/common/NgForOf#ngForTrackBy" class="code-anchor">ngForTrackBy</a></code>, respectively. That's how the directive learns that the list is <code>heroes</code> and the track-by function is <code><a href="api/core/IterableChangeRecord#trackById" class="code-anchor">trackById</a></code>.</p></li><li><p translation-result="on"> <code>NgFor</code> 指令在列表上循环，每个循环中都会设置和重置它自己的<em>上下文</em>对象上的属性。 这些属性包括但不限于 <code>index</code> 和 <code><a href="api/common/NgForOfContext#odd" class="code-anchor">odd</a></code> 以及一个特殊的属性名 <code>$implicit</code>（隐式变量）。</p><p translation-origin="off">As the <code>NgFor</code> directive loops through the list, it sets and resets properties of its own <em>context</em> object. These properties can include, but aren't limited to, <code>index</code>, <code><a href="api/common/NgForOfContext#odd" class="code-anchor">odd</a></code>, and a special property named <code>$implicit</code>.</p></li><li><p translation-result="on"> <code>let-i</code> 和 <code>let-odd</code> 变量是通过 <code>let i=index</code> 和 <code>let <a href="api/common/NgForOfContext#odd" class="code-anchor">odd</a>=<a href="api/common/NgForOfContext#odd" class="code-anchor">odd</a></code> 来定义的。 Angular 把它们设置为<em>上下文</em>对象中的 <code>index</code> 和 <code><a href="api/common/NgForOfContext#odd" class="code-anchor">odd</a></code> 属性的当前值。</p><p translation-origin="off">The <code>let-i</code> and <code>let-odd</code> variables were defined as <code>let i=index</code> and <code>let <a href="api/common/NgForOfContext#odd" class="code-anchor">odd</a>=<a href="api/common/NgForOfContext#odd" class="code-anchor">odd</a></code>. Angular sets them to the current value of the context's <code>index</code> and <code><a href="api/common/NgForOfContext#odd" class="code-anchor">odd</a></code> properties.</p></li><li><p translation-result="on">这里并没有指定 <code>let-hero</code> 的上下文属性。它的来源是隐式的。 Angular 将 <code>let-hero</code> 设置为此上下文中 <code>$implicit</code> 属性的值， 它是由 <code>NgFor</code> 用当前迭代中的英雄初始化的。</p><p translation-origin="off">The context property for <code>let-hero</code> wasn't specified. Its intended source is implicit. Angular sets <code>let-hero</code> to the value of the context's <code>$implicit</code> property, which <code>NgFor</code> has initialized with the hero for the current iteration.</p></li><li><p translation-result="on"><a href="api/common/NgForOf" title="API: NgFor">API 参考手册</a>中描述了 <code>NgFor</code> 指令的其它属性和上下文属性。</p><p translation-origin="off">The <a href="api/common/NgForOf" title="API: NgFor"><code>NgFor</code> API guide</a> describes additional <code>NgFor</code> directive properties and context properties.</p></li><li><p translation-result="on"> <code><a href="api/common/NgForOf" class="code-anchor">NgForOf</a></code> 指令实现了 <code>NgFor</code>。请到 <a href="api/common/NgForOf">NgForOf API 参考手册</a>中了解 <code><a href="api/common/NgForOf" class="code-anchor">NgForOf</a></code> 指令的更多属性及其上下文属性。</p><p translation-origin="off">The <code><a href="api/common/NgForOf" class="code-anchor">NgForOf</a></code> directive implements <code>NgFor</code>. Read more about additional <code><a href="api/common/NgForOf" class="code-anchor">NgForOf</a></code> directive properties and context properties in the <a href="api/common/NgForOf">NgForOf API reference</a>.</p></li></ul><h3 id="writing-your-own-structural-directives" translation-result="on">编写你自己的结构型指令<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#writing-your-own-structural-directives"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="writing-your-own-structural-directives">Writing your own structural directives<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#writing-your-own-structural-directives"><i class="material-icons">link</i></a></h3><p translation-result="on">当你编写自己的结构型指令时，也可以利用这些微语法机制。 例如，Angular 中的微语法允许你写成 <code>&lt;div *<a href="api/common/NgForOf" class="code-anchor">ngFor</a>="let <a href="api/core/IterableChangeRecord#item" class="code-anchor">item</a> of items"&gt;{{<a href="api/core/IterableChangeRecord#item" class="code-anchor">item</a>}}&lt;/div&gt;</code> 而不是 <code>&lt;ng-template <a href="api/common/NgForOf" class="code-anchor">ngFor</a> [<a href="api/common/NgForOf" class="code-anchor">ngForOf</a>]="items"&gt;&lt;div&gt;{{<a href="api/core/IterableChangeRecord#item" class="code-anchor">item</a>}}&lt;/div&gt;&lt;/ng-template&gt;</code>。 以下各节提供了有关约束、语法和微语法翻译方式的详细信息。</p><p translation-origin="off">These microsyntax mechanisms are also available to you when you write your own structural directives. For example, microsyntax in Angular allows you to write <code>&lt;div *<a href="api/common/NgForOf" class="code-anchor">ngFor</a>="let <a href="api/core/IterableChangeRecord#item" class="code-anchor">item</a> of items"&gt;{{<a href="api/core/IterableChangeRecord#item" class="code-anchor">item</a>}}&lt;/div&gt;</code> instead of <code>&lt;ng-template <a href="api/common/NgForOf" class="code-anchor">ngFor</a> [<a href="api/common/NgForOf" class="code-anchor">ngForOf</a>]="items"&gt;&lt;div&gt;{{<a href="api/core/IterableChangeRecord#item" class="code-anchor">item</a>}}&lt;/div&gt;&lt;/ng-template&gt;</code>. The following sections provide detailed information on constraints, grammar, and translation of microsyntax.</p><h3 id="constraints" translation-result="on">约束<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#constraints"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="constraints">Constraints<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#constraints"><i class="material-icons">link</i></a></h3><p translation-result="on">微语法必须满足以下要求：</p><p translation-origin="off">Microsyntax must meet the following requirements:</p><ul><li><p translation-result="on">它必须可被预先了解，以便 IDE 可以解析它而无需知道指令的底层语义或已存在哪些指令。</p><p translation-origin="off">It must be known ahead of time so that IDEs can parse it without knowing the underlying semantics of the directive or what directives are present.</p></li><li><p translation-result="on">它必须转换为 DOM 中的“键-值”属性。</p><p translation-origin="off">It must translate to key-value attributes in the DOM.</p></li></ul><h3 id="grammar" translation-result="on">语法<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#grammar"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="grammar">Grammar<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#grammar"><i class="material-icons">link</i></a></h3><p translation-result="on">当你编写自己的结构型指令时，请使用以下语法：</p><p translation-origin="off">When you write your own structural directives, use the following grammar:</p><code-example ng-version="9.0.0-rc.11"><div style="display:none">*:prefix="( :let | :expression ) (';' | ',')? ( :let | :as | :keyExp )*"</div><aio-code><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="pun">*:</span><span class="pln">prefix</span><span class="pun">=</span><span class="str">"( :let | :expression ) (';' | ',')? ( :let | :as | :keyExp )*"</span></code>
    </pre></aio-code></code-example><p translation-result="on">下表描述了微语法的每个组成部分。</p><p translation-origin="off">The following tables describe each portion of the microsyntax grammar.</p><table><tbody><tr><th></th><th></th></tr><tr><td><code>prefix</code></td><td><p translation-result="on">HTML 属性键（attribute key）</p><p translation-origin="off">HTML attribute key</p></td></tr><tr><td><code>key</code></td><td><p translation-result="on">HTML 属性键（attribute key）</p><p translation-origin="off">HTML attribute key</p></td></tr><tr><td><code>local</code></td><td><p translation-result="on">模板中使用的局部变量名</p><p translation-origin="off">local variable name used in the template</p></td></tr><tr><td><code>export</code></td><td><p translation-result="on">指令使用指定名称导出的值</p><p translation-origin="off">value exported by the directive under a given name</p></td></tr><tr><td><code>expression</code></td><td><p translation-result="on">标准 Angular 表达式</p><p translation-origin="off">standard Angular expression</p></td></tr></tbody></table><table><tbody><tr><th></th></tr><tr><td colspan="3"><code>keyExp = :key ":"? :expression ("as" :local)? ";"? </code></td></tr><tr><td colspan="3"><code>let = "let" :local "=" :export ";"?</code></td></tr><tr><td colspan="3"><code>as = :export "as" :local ";"?</code></td></tr></tbody></table><h3 id="translation" translation-result="on">翻译<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#translation"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="translation">Translation<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#translation"><i class="material-icons">link</i></a></h3><p translation-result="on">将微语法转换为常规的绑定语法，如下所示：</p><p translation-origin="off">A microsyntax is translated to the normal binding syntax as follows:</p><table><tbody><tr><th><p translation-result="on">微语法</p><p translation-origin="off">Microsyntax</p></th><th><p translation-result="on">翻译结果</p><p translation-origin="off">Translation</p></th></tr><tr><td><p translation-result="on"><code>prefix</code> 和裸<code>表达式</code></p><p translation-origin="off"><code>prefix</code> and naked <code>expression</code></p></td><td><code>[prefix]="expression"</code></td></tr><tr><td><code>keyExp</code></td><td><p translation-result="on"><code>[prefixKey] "表达式"
    (let-prefixKey="export")</code> <br>注意 <code>prefix</code> 已经加成了 <code>key</code> </p><p translation-origin="off"><code>[prefixKey] "expression"
    (let-prefixKey="export")</code> <br>Notice that the <code>prefix</code> is added to the <code>key</code> </p></td></tr><tr><td><code>let</code></td><td><code>let-local="export"</code></td></tr></tbody></table><h3 id="microsyntax-examples" translation-result="on">微语法样例<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#microsyntax-examples"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="microsyntax-examples">Microsyntax examples<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#microsyntax-examples"><i class="material-icons">link</i></a></h3><p translation-result="on">下表说明了 Angular 会如何解开微语法。</p><p translation-origin="off">The following table demonstrates how Angular desugars microsyntax.</p><table><tbody><tr><th><p translation-result="on">微语法</p><p translation-origin="off">Microsyntax</p></th><th><p translation-result="on">解语法糖后</p><p translation-origin="off">Desugared</p></th></tr><tr><td><code>*<a href="api/common/NgForOf" class="code-anchor">ngFor</a>="let <a href="api/core/IterableChangeRecord#item" class="code-anchor">item</a> of [1,2,3]"</code></td><td><code>&lt;ng-template <a href="api/common/NgForOf" class="code-anchor">ngFor</a> let-item [<a href="api/common/NgForOf" class="code-anchor">ngForOf</a>]="[1,2,3]"&gt;</code></td></tr><tr><td><code>*<a href="api/common/NgForOf" class="code-anchor">ngFor</a>="let <a href="api/core/IterableChangeRecord#item" class="code-anchor">item</a> of [1,2,3] as items; trackBy: myTrack; index as i"</code></td><td><code>&lt;ng-template <a href="api/common/NgForOf" class="code-anchor">ngFor</a> let-item [<a href="api/common/NgForOf" class="code-anchor">ngForOf</a>]="[1,2,3]" let-items="<a href="api/common/NgForOf" class="code-anchor">ngForOf</a>" [<a href="api/common/NgForOf#ngForTrackBy" class="code-anchor">ngForTrackBy</a>]="myTrack" let-i="index"&gt;</code> </td></tr><tr><td><code>*<a href="api/common/NgIf" class="code-anchor">ngIf</a>="exp"</code></td><td><code>&lt;ng-template [<a href="api/common/NgIf" class="code-anchor">ngIf</a>]="exp"&gt;</code></td></tr><tr><td><code>*<a href="api/common/NgIf" class="code-anchor">ngIf</a>="exp as value"</code></td><td><code>&lt;ng-template [<a href="api/common/NgIf" class="code-anchor">ngIf</a>]="exp" let-value="<a href="api/common/NgIf" class="code-anchor">ngIf</a>"&gt;</code></td></tr></tbody></table><p translation-result="on">这些微语法机制在你写自己的结构型指令时也同样有效，参考 <a href="https://github.com/angular/angular/blob/master/packages/common/src/directives/ng_if.ts" title="Source: NgIf"><code>NgIf</code> 的源码</a> 和 <a href="https://github.com/angular/angular/blob/master/packages/common/src/directives/ng_for_of.ts" title="Source: NgFor"><code>NgFor</code> 的源码</a> 可以学到更多。 <a id="template-input-variable"></a></p><p translation-origin="off">Studying the <a href="https://github.com/angular/angular/blob/master/packages/common/src/directives/ng_if.ts" title="Source: NgIf">source code for <code>NgIf</code></a>and <a href="https://github.com/angular/angular/blob/master/packages/common/src/directives/ng_for_of.ts" title="Source: NgForOf"><code>NgForOf</code></a>is a great way to learn more.</p><a id="template-input-variables"></a><h2 id="template-input-variable" translation-result="on">模板输入变量<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#template-input-variable"><i class="material-icons">link</i></a></h2><h2 translation-origin="off" id="template-input-variable">Template input variable<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#template-input-variable"><i class="material-icons">link</i></a></h2><p translation-result="on"><em>模板输入变量</em>是这样一种变量，你可以<em>在单个实例的模板中</em>引用它的值。 这个例子中有好几个模板输入变量：<code>hero</code>、<code>i</code> 和 <code><a href="api/common/NgForOfContext#odd" class="code-anchor">odd</a></code>。 它们都是用 <code>let</code> 作为前导关键字。</p><p translation-origin="off">A <em>template input variable</em> is a variable whose value you can reference <em>within</em> a single instance of the template. There are several such variables in this example: <code>hero</code>, <code>i</code>, and <code><a href="api/common/NgForOfContext#odd" class="code-anchor">odd</a></code>. All are preceded by the keyword <code>let</code>.</p><p translation-result="on"><em>模板输入变量</em>和<a href="guide/template-syntax#ref-vars">模板引用变量</a>是<strong>不同的</strong>，无论是在<em>语义</em>上还是<em>语法</em>上。</p><p translation-origin="off">A <em>template input variable</em> is <strong><em>not</em></strong> the same as a <a href="guide/template-syntax#ref-vars">template <em>reference</em> variable</a>, neither <em>semantically</em> nor <em>syntactically</em>.</p><p translation-result="on">你使用 <code>let</code> 关键字（如 <code>let hero</code>）在模板中声明一个模板<em>输入</em>变量。 这个变量的范围被限制在所重复模板的<em>单一实例</em>上。 事实上，你可以在其它结构型指令中使用同样的变量名。</p><p translation-origin="off">You declare a template <em>input</em> variable using the <code>let</code> keyword (<code>let hero</code>). The variable's scope is limited to a <em>single instance</em> of the repeated template. You can use the same variable name again in the definition of other structural directives.</p><p translation-result="on">而声明模板<em>引用</em>变量使用的是给变量名加 <code>#</code> 前缀的方式（<code>#var</code>）。 一个<em>引用</em>变量引用的是它所附着到的元素、组件或指令。它可以在<em>整个模板</em>的<em>任意位置</em>访问。</p><p translation-origin="off">You declare a template <em>reference</em> variable by prefixing the variable name with <code>#</code> (<code>#var</code>). A <em>reference</em> variable refers to its attached element, component or directive. It can be accessed <em>anywhere</em> in the <em>entire template</em>.</p><p translation-result="on">模板<em>输入</em>变量和<em>引用</em>变量具有各自独立的命名空间。<code>let hero</code> 中的 <code>hero</code> 和 <code>#hero</code> 中的 <code>hero</code> 并不是同一个变量。</p><p translation-origin="off">Template <em>input</em> and <em>reference</em> variable names have their own namespaces. The <code>hero</code> in <code>let hero</code> is never the same variable as the <code>hero</code> declared as <code>#hero</code>.</p><a id="one-per-element"></a><h2 id="one-structural-directive-per-host-element" translation-result="on">每个宿主元素上只能有一个结构型指令<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#one-structural-directive-per-host-element"><i class="material-icons">link</i></a></h2><h2 translation-origin="off" id="one-structural-directive-per-host-element">One structural directive per host element<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#one-structural-directive-per-host-element"><i class="material-icons">link</i></a></h2><p translation-result="on">有时你会希望只有当特定的条件为真时才重复渲染一个 HTML 块。 你可能试过把 <code>*<a href="api/common/NgForOf" class="code-anchor">ngFor</a></code> 和 <code>*<a href="api/common/NgIf" class="code-anchor">ngIf</a></code> 放在同一个宿主元素上，但 Angular 不允许。这是因为你在一个元素上只能放一个<em>结构型</em>指令。</p><p translation-origin="off">Someday you'll want to repeat a block of HTML but only when a particular condition is true. You'll <em>try</em> to put both an <code>*<a href="api/common/NgForOf" class="code-anchor">ngFor</a></code> and an <code>*<a href="api/common/NgIf" class="code-anchor">ngIf</a></code> on the same host element. Angular won't let you. You may apply only one <em>structural</em> directive to an element.</p><p translation-result="on">原因很简单。结构型指令可能会对宿主元素及其子元素做很复杂的事。当两个指令放在同一个元素上时，谁先谁后？<code><a href="api/common/NgIf" class="code-anchor">NgIf</a></code> 优先还是 <code>NgFor</code> 优先？<code><a href="api/common/NgIf" class="code-anchor">NgIf</a></code> 可以取消 <code>NgFor</code> 的效果吗？ 如果要这样做，Angular 应该如何把这种能力泛化，以取消其它结构型指令的效果呢？</p><p translation-origin="off">The reason is simplicity. Structural directives can do complex things with the host element and its descendents. When two directives lay claim to the same host element, which one takes precedence? Which should go first, the <code><a href="api/common/NgIf" class="code-anchor">NgIf</a></code> or the <code>NgFor</code>? Can the <code><a href="api/common/NgIf" class="code-anchor">NgIf</a></code> cancel the effect of the <code>NgFor</code>? If so (and it seems like it should be so), how should Angular generalize the ability to cancel for other structural directives?</p><p translation-result="on">对这些问题，没有办法简单回答。而禁止多个结构型指令则可以简单地解决这个问题。 这种情况下有一个简单的解决方案：把 <code>*<a href="api/common/NgIf" class="code-anchor">ngIf</a></code> 放在一个"容器"元素上，再包装进 <code>*<a href="api/common/NgForOf" class="code-anchor">ngFor</a></code> 元素。 这个元素可以使用<a href="guide/structural-directives#ngcontainer"><code>ng-container</code></a>，以免引入一个新的 HTML 层级。</p><p translation-origin="off">There are no easy answers to these questions. Prohibiting multiple structural directives makes them moot. There's an easy solution for this use case: put the <code>*<a href="api/common/NgIf" class="code-anchor">ngIf</a></code> on a container element that wraps the <code>*<a href="api/common/NgForOf" class="code-anchor">ngFor</a></code> element. One or both elements can be an <a href="guide/structural-directives#ngcontainer"><code>ng-container</code></a>so you don't have to introduce extra levels of HTML.</p><a id="ngSwitch"></a><h2 id="inside-ngswitch-directives" translation-result="on"><code><a href="api/common/NgSwitch" class="code-anchor">NgSwitch</a></code> 内幕<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#inside-ngswitch-directives"><i class="material-icons">link</i></a></h2><h2 translation-origin="off" id="inside-ngswitch-directives">Inside <em>NgSwitch</em> directives<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#inside-ngswitch-directives"><i class="material-icons">link</i></a></h2><p translation-result="on">Angular 的 <code><a href="api/common/NgSwitch" class="code-anchor">NgSwitch</a></code> 实际上是一组相互合作的指令：<code><a href="api/common/NgSwitch" class="code-anchor">NgSwitch</a></code>、<code><a href="api/common/NgSwitchCase" class="code-anchor">NgSwitchCase</a></code> 和 <code><a href="api/common/NgSwitchDefault" class="code-anchor">NgSwitchDefault</a></code>。</p><p translation-origin="off">The Angular <em>NgSwitch</em> is actually a set of cooperating directives: <code><a href="api/common/NgSwitch" class="code-anchor">NgSwitch</a></code>, <code><a href="api/common/NgSwitchCase" class="code-anchor">NgSwitchCase</a></code>, and <code><a href="api/common/NgSwitchDefault" class="code-anchor">NgSwitchDefault</a></code>.</p><p translation-result="on">例子如下：</p><p translation-origin="off">Here's an example.</p><code-example path="structural-directives/src/app/app.component.html" header="src/app/app.component.html (ngswitch)" region="ngswitch" ng-version="9.0.0-rc.11"><div style="display:none">&lt;div [<a href="api/common/NgSwitch" class="code-anchor">ngSwitch</a>]="hero?.emotion"&gt; &lt;app-happy-hero *<a href="api/common/NgSwitchCase" class="code-anchor">ngSwitchCase</a>="'happy'" [hero]="hero"&gt;&lt;/app-happy-hero&gt; &lt;app-sad-hero *<a href="api/common/NgSwitchCase" class="code-anchor">ngSwitchCase</a>="'sad'" [hero]="hero"&gt;&lt;/app-sad-hero&gt; &lt;app-confused-hero *<a href="api/common/NgSwitchCase" class="code-anchor">ngSwitchCase</a>="'confused'" [hero]="hero"&gt;&lt;/app-confused-hero&gt; &lt;app-unknown-hero *<a href="api/common/NgSwitchDefault" class="code-anchor">ngSwitchDefault</a> [hero]="hero"&gt;&lt;/app-unknown-hero&gt; &lt;/div&gt;</div><header class="ng-star-inserted">src/app/app.component.html (ngswitch)</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from src/app/app.component.html (ngswitch)">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="tag">&lt;div</span><span class="pln"> [</span><a href="api/common/NgSwitch" class="code-anchor"><span class="atn">ngSwitch</span></a><span class="pln">]</span><span class="pun">=</span><span class="atv">"hero?.emotion"</span><span class="tag">&gt;</span><span class="pln">
  </span><span class="tag">&lt;app-happy-hero</span><span class="pln">    *</span><a href="api/common/NgSwitchCase" class="code-anchor"><span class="atn">ngSwitchCase</span></a><span class="pun">=</span><span class="atv">"'happy'"</span><span class="pln">    [</span><span class="atn">hero</span><span class="pln">]</span><span class="pun">=</span><span class="atv">"hero"</span><span class="tag">&gt;&lt;/app-happy-hero&gt;</span><span class="pln">
  </span><span class="tag">&lt;app-sad-hero</span><span class="pln">      *</span><a href="api/common/NgSwitchCase" class="code-anchor"><span class="atn">ngSwitchCase</span></a><span class="pun">=</span><span class="atv">"'sad'"</span><span class="pln">      [</span><span class="atn">hero</span><span class="pln">]</span><span class="pun">=</span><span class="atv">"hero"</span><span class="tag">&gt;&lt;/app-sad-hero&gt;</span><span class="pln">
  </span><span class="tag">&lt;app-confused-hero</span><span class="pln"> *</span><a href="api/common/NgSwitchCase" class="code-anchor"><span class="atn">ngSwitchCase</span></a><span class="pun">=</span><span class="atv">"'confused'"</span><span class="pln"> [</span><span class="atn">hero</span><span class="pln">]</span><span class="pun">=</span><span class="atv">"hero"</span><span class="tag">&gt;&lt;/app-confused-hero&gt;</span><span class="pln">
  </span><span class="tag">&lt;app-unknown-hero</span><span class="pln">  *</span><a href="api/common/NgSwitchDefault" class="code-anchor"><span class="atn">ngSwitchDefault</span></a><span class="pln">           [</span><span class="atn">hero</span><span class="pln">]</span><span class="pun">=</span><span class="atv">"hero"</span><span class="tag">&gt;&lt;/app-unknown-hero&gt;</span><span class="pln">
</span><span class="tag">&lt;/div&gt;</span></code>
    </pre></aio-code></code-example><p translation-result="on">一个值(<code>hero.emotion</code>)被被赋值给了 <code><a href="api/common/NgSwitch" class="code-anchor">NgSwitch</a></code>，以决定要显示哪一个分支。</p><p translation-origin="off">The switch value assigned to <code><a href="api/common/NgSwitch" class="code-anchor">NgSwitch</a></code> (<code>hero.emotion</code>) determines which (if any) of the switch cases are displayed.</p><p translation-result="on"><code><a href="api/common/NgSwitch" class="code-anchor">NgSwitch</a></code> 本身不是结构型指令，而是一个<em>属性型</em>指令，它控制其它两个 switch 指令的行为。 这也就是为什么你要写成 <code>[<a href="api/common/NgSwitch" class="code-anchor">ngSwitch</a>]</code> 而不是 <code>*<a href="api/common/NgSwitch" class="code-anchor">ngSwitch</a></code> 的原因。</p><p translation-origin="off"><code><a href="api/common/NgSwitch" class="code-anchor">NgSwitch</a></code> itself is not a structural directive. It's an <em>attribute</em> directive that controls the behavior of the other two switch directives. That's why you write <code>[<a href="api/common/NgSwitch" class="code-anchor">ngSwitch</a>]</code>, never <code>*<a href="api/common/NgSwitch" class="code-anchor">ngSwitch</a></code>.</p><p translation-result="on"><code><a href="api/common/NgSwitchCase" class="code-anchor">NgSwitchCase</a></code> 和 <code><a href="api/common/NgSwitchDefault" class="code-anchor">NgSwitchDefault</a></code> <em>都是</em>结构型指令。 因此你要使用星号（<code>*</code>）前缀来把它们附着到元素上。 <code><a href="api/common/NgSwitchCase" class="code-anchor">NgSwitchCase</a></code> 会在它的值匹配上选项值的时候显示它的宿主元素。 <code><a href="api/common/NgSwitchDefault" class="code-anchor">NgSwitchDefault</a></code> 则会当没有兄弟 <code><a href="api/common/NgSwitchCase" class="code-anchor">NgSwitchCase</a></code> 匹配上时显示它的宿主元素。</p><p translation-origin="off"><code><a href="api/common/NgSwitchCase" class="code-anchor">NgSwitchCase</a></code> and <code><a href="api/common/NgSwitchDefault" class="code-anchor">NgSwitchDefault</a></code> <em>are</em> structural directives. You attach them to elements using the asterisk (*) prefix notation. An <code><a href="api/common/NgSwitchCase" class="code-anchor">NgSwitchCase</a></code> displays its host element when its value matches the switch value. The <code><a href="api/common/NgSwitchDefault" class="code-anchor">NgSwitchDefault</a></code> displays its host element when no sibling <code><a href="api/common/NgSwitchCase" class="code-anchor">NgSwitchCase</a></code> matches the switch value.</p><div class="alert is-helpful"><p translation-result="on">指令所在的元素就是它的<strong>宿主</strong>元素。 <code>&lt;happy-hero&gt;</code> 是 <code>*<a href="api/common/NgSwitchCase" class="code-anchor">ngSwitchCase</a></code> 的宿主元素。 <code>&lt;unknown-hero&gt;</code> 是 <code>*<a href="api/common/NgSwitchDefault" class="code-anchor">ngSwitchDefault</a></code> 的宿主元素。</p><p translation-origin="off">The element to which you apply a directive is its <em>host</em> element. The <code>&lt;happy-hero&gt;</code> is the host element for the happy <code>*<a href="api/common/NgSwitchCase" class="code-anchor">ngSwitchCase</a></code>. The <code>&lt;unknown-hero&gt;</code> is the host element for the <code>*<a href="api/common/NgSwitchDefault" class="code-anchor">ngSwitchDefault</a></code>.</p></div><p translation-result="on">像其它的结构型指令一样，<code><a href="api/common/NgSwitchCase" class="code-anchor">NgSwitchCase</a></code> 和 <code><a href="api/common/NgSwitchDefault" class="code-anchor">NgSwitchDefault</a></code> 也可以解开语法糖，变成 <code>&lt;ng-template&gt;</code> 的形式。</p><p translation-origin="off">As with other structural directives, the <code><a href="api/common/NgSwitchCase" class="code-anchor">NgSwitchCase</a></code> and <code><a href="api/common/NgSwitchDefault" class="code-anchor">NgSwitchDefault</a></code> can be desugared into the <code>&lt;ng-template&gt;</code> element form.</p><code-example path="structural-directives/src/app/app.component.html" header="src/app/app.component.html (ngswitch-template)" region="ngswitch-template" ng-version="9.0.0-rc.11"><div style="display:none">&lt;div [<a href="api/common/NgSwitch" class="code-anchor">ngSwitch</a>]="hero?.emotion"&gt; &lt;ng-template [<a href="api/common/NgSwitchCase" class="code-anchor">ngSwitchCase</a>]="'happy'"&gt; &lt;app-happy-hero [hero]="hero"&gt;&lt;/app-happy-hero&gt; &lt;/ng-template&gt; &lt;ng-template [<a href="api/common/NgSwitchCase" class="code-anchor">ngSwitchCase</a>]="'sad'"&gt; &lt;app-sad-hero [hero]="hero"&gt;&lt;/app-sad-hero&gt; &lt;/ng-template&gt; &lt;ng-template [<a href="api/common/NgSwitchCase" class="code-anchor">ngSwitchCase</a>]="'confused'"&gt; &lt;app-confused-hero [hero]="hero"&gt;&lt;/app-confused-hero&gt; &lt;/ng-template &gt; &lt;ng-template <a href="api/common/NgSwitchDefault" class="code-anchor">ngSwitchDefault</a>&gt; &lt;app-unknown-hero [hero]="hero"&gt;&lt;/app-unknown-hero&gt; &lt;/ng-template&gt; &lt;/div&gt;</div><header class="ng-star-inserted">src/app/app.component.html (ngswitch-template)</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from src/app/app.component.html (ngswitch-template)">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="tag">&lt;div</span><span class="pln"> [</span><a href="api/common/NgSwitch" class="code-anchor"><span class="atn">ngSwitch</span></a><span class="pln">]</span><span class="pun">=</span><span class="atv">"hero?.emotion"</span><span class="tag">&gt;</span><span class="pln">
  </span><span class="tag">&lt;ng-template</span><span class="pln"> [</span><a href="api/common/NgSwitchCase" class="code-anchor"><span class="atn">ngSwitchCase</span></a><span class="pln">]</span><span class="pun">=</span><span class="atv">"'happy'"</span><span class="tag">&gt;</span><span class="pln">
    </span><span class="tag">&lt;app-happy-hero</span><span class="pln"> [</span><span class="atn">hero</span><span class="pln">]</span><span class="pun">=</span><span class="atv">"hero"</span><span class="tag">&gt;&lt;/app-happy-hero&gt;</span><span class="pln">
  </span><span class="tag">&lt;/ng-template&gt;</span><span class="pln">
  </span><span class="tag">&lt;ng-template</span><span class="pln"> [</span><a href="api/common/NgSwitchCase" class="code-anchor"><span class="atn">ngSwitchCase</span></a><span class="pln">]</span><span class="pun">=</span><span class="atv">"'sad'"</span><span class="tag">&gt;</span><span class="pln">
    </span><span class="tag">&lt;app-sad-hero</span><span class="pln"> [</span><span class="atn">hero</span><span class="pln">]</span><span class="pun">=</span><span class="atv">"hero"</span><span class="tag">&gt;&lt;/app-sad-hero&gt;</span><span class="pln">
  </span><span class="tag">&lt;/ng-template&gt;</span><span class="pln">
  </span><span class="tag">&lt;ng-template</span><span class="pln"> [</span><a href="api/common/NgSwitchCase" class="code-anchor"><span class="atn">ngSwitchCase</span></a><span class="pln">]</span><span class="pun">=</span><span class="atv">"'confused'"</span><span class="tag">&gt;</span><span class="pln">
    </span><span class="tag">&lt;app-confused-hero</span><span class="pln"> [</span><span class="atn">hero</span><span class="pln">]</span><span class="pun">=</span><span class="atv">"hero"</span><span class="tag">&gt;&lt;/app-confused-hero&gt;</span><span class="pln">
  </span><span class="tag">&lt;/ng-template</span><span class="pln"> </span><span class="tag">&gt;</span><span class="pln">
  </span><span class="tag">&lt;ng-template</span><span class="pln"> </span><a href="api/common/NgSwitchDefault" class="code-anchor"><span class="atn">ngSwitchDefault</span></a><span class="tag">&gt;</span><span class="pln">
    </span><span class="tag">&lt;app-unknown-hero</span><span class="pln"> [</span><span class="atn">hero</span><span class="pln">]</span><span class="pun">=</span><span class="atv">"hero"</span><span class="tag">&gt;&lt;/app-unknown-hero&gt;</span><span class="pln">
  </span><span class="tag">&lt;/ng-template&gt;</span><span class="pln">
</span><span class="tag">&lt;/div&gt;</span></code>
    </pre></aio-code></code-example><a id="prefer-asterisk"></a><h2 id="prefer-the-asterisk--syntax" translation-result="on">优先使用星号（<code>*</code>）语法<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#prefer-the-asterisk--syntax"><i class="material-icons">link</i></a></h2><h2 translation-origin="off" id="prefer-the-asterisk--syntax">Prefer the asterisk (*) syntax.<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#prefer-the-asterisk--syntax"><i class="material-icons">link</i></a></h2><p translation-result="on">星号（<code>*</code>）语法比不带语法糖的形式更加清晰。 如果找不到单一的元素来应用该指令，可以使用<a href="guide/structural-directives#ng-container">&lt;ng-container&gt;</a>作为该指令的容器。</p><p translation-origin="off">The asterisk (*) syntax is more clear than the desugared form. Use <a href="guide/structural-directives#ng-container">&lt;ng-container&gt;</a> when there's no single element to host the directive.</p><p translation-result="on">虽然很少有理由在模板中使用结构型指令的<em>属性</em>形式和<em>元素</em>形式，但这些幕后知识仍然是很重要的，即：Angular 会创建 <code>&lt;ng-template&gt;</code>，还要了解它的工作原理。 当需要<a href="guide/structural-directives#unless">写自己的结构型指令</a>时，你就要使用 <code>&lt;ng-template&gt;</code>。</p><p translation-origin="off">While there's rarely a good reason to apply a structural directive in template <em>attribute</em> or <em>element</em> form, it's still important to know that Angular creates a <code>&lt;ng-template&gt;</code> and to understand how it works. You'll refer to the <code>&lt;ng-template&gt;</code> when you <a href="guide/structural-directives#unless">write your own structural directive</a>.</p><a id="template"></a><h2 id="the-ng-template" translation-result="on"><em>&lt;ng-template&gt;</em>元素<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#the-ng-template"><i class="material-icons">link</i></a></h2><h2 translation-origin="off" id="the-ng-template">The <em>&lt;ng-template&gt;</em><a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#the-ng-template"><i class="material-icons">link</i></a></h2><p translation-result="on">&lt;ng-template&gt;是一个 Angular 元素，用来渲染 HTML。 它永远不会直接显示出来。 事实上，在渲染视图之前，Angular 会把 <code>&lt;ng-template&gt;</code> 及其内容<em>替换为</em>一个注释。</p><p translation-origin="off">The &lt;ng-template&gt; is an Angular element for rendering HTML. It is never displayed directly. In fact, before rendering the view, Angular <em>replaces</em> the <code>&lt;ng-template&gt;</code> and its contents with a comment.</p><p translation-result="on">如果没有使用结构型指令，而仅仅把一些别的元素包装进 <code>&lt;ng-template&gt;</code> 中，那些元素就是不可见的。 在下面的这个短语"Hip! Hip! Hooray!"中，中间的这个 "Hip!"（欢呼声） 就是如此。</p><p translation-origin="off">If there is no structural directive and you merely wrap some elements in a <code>&lt;ng-template&gt;</code>, those elements disappear. That's the fate of the middle "Hip!" in the phrase "Hip! Hip! Hooray!".</p><code-example path="structural-directives/src/app/app.component.html" header="src/app/app.component.html (template-tag)" region="template-tag" ng-version="9.0.0-rc.11"><div style="display:none">&lt;p&gt;Hip!&lt;/p&gt; &lt;ng-template&gt; &lt;p&gt;Hip!&lt;/p&gt; &lt;/ng-template&gt; &lt;p&gt;Hooray!&lt;/p&gt;</div><header class="ng-star-inserted">src/app/app.component.html (template-tag)</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from src/app/app.component.html (template-tag)">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="tag">&lt;p&gt;</span><span class="pln">Hip!</span><span class="tag">&lt;/p&gt;</span><span class="pln">
</span><span class="tag">&lt;ng-template&gt;</span><span class="pln">
  </span><span class="tag">&lt;p&gt;</span><span class="pln">Hip!</span><span class="tag">&lt;/p&gt;</span><span class="pln">
</span><span class="tag">&lt;/ng-template&gt;</span><span class="pln">
</span><span class="tag">&lt;p&gt;</span><span class="pln">Hooray!</span><span class="tag">&lt;/p&gt;</span></code>
    </pre></aio-code></code-example><p translation-result="on">Angular 抹掉了中间的那个 "Hip!" ，让欢呼声显得不再那么热烈了。</p><p translation-origin="off">Angular erases the middle "Hip!", leaving the cheer a bit less enthusiastic.</p><div class="lightbox"><img src="generated/images/guide/structural-directives/template-rendering.png" alt="template tag rendering" width="520" height="94"></div><p translation-result="on">结构型指令会让 <code>&lt;ng-template&gt;</code> 正常工作，在你<a href="guide/structural-directives#unless">写自己的结构型指令</a>时就会看到这一点。</p><p translation-origin="off">A structural directive puts a <code>&lt;ng-template&gt;</code> to work as you'll see when you <a href="guide/structural-directives#unless">write your own structural directive</a>.</p><a id="ngcontainer"></a> <a id="ng-container"></a><h2 id="group-sibling-elements-with-ng-container" translation-result="on">使用&lt;ng-container&gt;把一些兄弟元素归为一组<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#group-sibling-elements-with-ng-container"><i class="material-icons">link</i></a></h2><h2 translation-origin="off" id="group-sibling-elements-with-ng-container">Group sibling elements with &lt;ng-container&gt;<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#group-sibling-elements-with-ng-container"><i class="material-icons">link</i></a></h2><p translation-result="on">通常都需要一个<em>根</em>元素作为结构型指令的宿主。 列表元素（<code>&lt;li&gt;</code>）就是一个典型的供 <code>NgFor</code> 使用的宿主元素。</p><p translation-origin="off">There's often a <em>root</em> element that can and should host the structural directive. The list element (<code>&lt;li&gt;</code>) is a typical host element of an <code>NgFor</code> repeater.</p><code-example path="structural-directives/src/app/app.component.html" header="src/app/app.component.html (ngfor-li)" region="ngfor-li" ng-version="9.0.0-rc.11"><div style="display:none">&lt;li *<a href="api/common/NgForOf" class="code-anchor">ngFor</a>="let hero of heroes"&gt;{{hero.name}}&lt;/li&gt;</div><header class="ng-star-inserted">src/app/app.component.html (ngfor-li)</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from src/app/app.component.html (ngfor-li)">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="tag">&lt;li</span><span class="pln"> *</span><a href="api/common/NgForOf" class="code-anchor"><span class="atn">ngFor</span></a><span class="pun">=</span><span class="atv">"let hero of heroes"</span><span class="tag">&gt;</span><span class="pln">{{hero.name}}</span><span class="tag">&lt;/li&gt;</span></code>
    </pre></aio-code></code-example><p translation-result="on">当没有这样一个单一的宿主元素时，你就可以把这些内容包裹在一个原生的 HTML 容器元素中，比如 <code>&lt;div&gt;</code>，并且把结构型指令附加到这个"包裹"上。</p><p translation-origin="off">When there isn't a host element, you can usually wrap the content in a native HTML container element, such as a <code>&lt;div&gt;</code>, and attach the directive to that wrapper.</p><code-example path="structural-directives/src/app/app.component.html" header="src/app/app.component.html (ngif)" region="ngif" ng-version="9.0.0-rc.11"><div style="display:none">&lt;div *<a href="api/common/NgIf" class="code-anchor">ngIf</a>="hero" class="name"&gt;{{hero.name}}&lt;/div&gt;</div><header class="ng-star-inserted">src/app/app.component.html (ngif)</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from src/app/app.component.html (ngif)">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="tag">&lt;div</span><span class="pln"> *</span><a href="api/common/NgIf" class="code-anchor"><span class="atn">ngIf</span></a><span class="pun">=</span><span class="atv">"hero"</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"name"</span><span class="tag">&gt;</span><span class="pln">{{hero.name}}</span><span class="tag">&lt;/div&gt;</span></code>
    </pre></aio-code></code-example><p translation-result="on">但引入另一个容器元素（通常是 <code>&lt;span&gt;</code> 或 <code>&lt;div&gt;</code>）来把一些元素归到一个单一的<em>根元素</em>下，通常也会带来问题。注意，是"通常"而不是"总会"。</p><p translation-origin="off">Introducing another container element—typically a <code>&lt;span&gt;</code> or <code>&lt;div&gt;</code>—to group the elements under a single <em>root</em> is usually harmless. <em>Usually</em> ... but not <em>always</em>.</p><p translation-result="on">这种用于分组的元素可能会破坏模板的外观表现，因为 CSS 的样式既不曾期待也不会接受这种新的元素布局。 比如，假设你有下列分段布局。</p><p translation-origin="off">The grouping element may break the template appearance because CSS styles neither expect nor accommodate the new layout. For example, suppose you have the following paragraph layout.</p><code-example path="structural-directives/src/app/app.component.html" header="src/app/app.component.html (ngif-span)" region="ngif-span" ng-version="9.0.0-rc.11"><div style="display:none">&lt;p&gt; I turned the corner &lt;span *<a href="api/common/NgIf" class="code-anchor">ngIf</a>="hero"&gt; and saw {{hero.name}}. I waved &lt;/span&gt; and continued on my way. &lt;/p&gt;</div><header class="ng-star-inserted">src/app/app.component.html (ngif-span)</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from src/app/app.component.html (ngif-span)">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="tag">&lt;p&gt;</span><span class="pln">
  I turned the corner
  </span><span class="tag">&lt;span</span><span class="pln"> *</span><a href="api/common/NgIf" class="code-anchor"><span class="atn">ngIf</span></a><span class="pun">=</span><span class="atv">"hero"</span><span class="tag">&gt;</span><span class="pln">
    and saw {{hero.name}}. I waved
  </span><span class="tag">&lt;/span&gt;</span><span class="pln">
  and continued on my way.
</span><span class="tag">&lt;/p&gt;</span></code>
    </pre></aio-code></code-example><p translation-result="on">而你的 CSS 样式规则是应用于 <code>&lt;p&gt;</code> 元素下的 <code>&lt;span&gt;</code> 的。</p><p translation-origin="off">You also have a CSS style rule that happens to apply to a <code>&lt;span&gt;</code> within a <code>&lt;p&gt;</code>aragraph.</p><code-example path="structural-directives/src/app/app.component.css" header="src/app/app.component.css (p-span)" region="p-span" ng-version="9.0.0-rc.11"><div style="display:none">p span { color: red; font-size: 70%; }</div><header class="ng-star-inserted">src/app/app.component.css (p-span)</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from src/app/app.component.css (p-span)">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="pln">p span </span><span class="pun">{</span><span class="pln"> color</span><span class="pun">:</span><span class="pln"> red</span><span class="pun">;</span><span class="pln"> font</span><span class="pun">-</span><span class="pln">size</span><span class="pun">:</span><span class="pln"> </span><span class="lit">70</span><span class="pun">%;</span><span class="pln"> </span><span class="pun">}</span></code>
    </pre></aio-code></code-example><p translation-result="on">这样渲染出来的段落就会非常奇怪。</p><p translation-origin="off">The constructed paragraph renders strangely.</p><div class="lightbox"><img src="generated/images/guide/structural-directives/bad-paragraph.png" alt="spanned paragraph with bad style" width="452" height="15"></div><p translation-result="on">本来为其它地方准备的 <code>p span</code> 样式，被意外的应用到了这里。</p><p translation-origin="off">The <code>p span</code> style, intended for use elsewhere, was inadvertently applied here.</p><p translation-result="on">另一个问题是：有些 HTML 元素需要所有的直属下级都具有特定的类型。 比如，<code>&lt;select&gt;</code> 元素要求直属下级必须为 <code>&lt;option&gt;</code>，那就没办法把这些选项包装进 <code>&lt;div&gt;</code> 或 <code>&lt;span&gt;</code> 中。</p><p translation-origin="off">Another problem: some HTML elements require all immediate children to be of a specific type. For example, the <code>&lt;select&gt;</code> element requires <code>&lt;option&gt;</code> children. You can't wrap the <em>options</em> in a conditional <code>&lt;div&gt;</code> or a <code>&lt;span&gt;</code>.</p><p translation-result="on">如果这样做：</p><p translation-origin="off">When you try this,</p><code-example path="structural-directives/src/app/app.component.html" header="src/app/app.component.html (select-span)" region="select-span" ng-version="9.0.0-rc.11"><div style="display:none">&lt;div&gt; Pick your favorite hero (&lt;label&gt;&lt;input type="checkbox" checked (change)="showSad = !showSad"&gt;show sad&lt;/label&gt;) &lt;/div&gt; &lt;select [(<a href="api/forms/NgModel" class="code-anchor">ngModel</a>)]="hero"&gt; &lt;span *<a href="api/common/NgForOf" class="code-anchor">ngFor</a>="let h of heroes"&gt; &lt;span *<a href="api/common/NgIf" class="code-anchor">ngIf</a>="showSad || h.emotion !== 'sad'"&gt; &lt;option [ngValue]="h"&gt;{{h.name}} ({{h.emotion}})&lt;/option&gt; &lt;/span&gt; &lt;/span&gt; &lt;/select&gt;</div><header class="ng-star-inserted">src/app/app.component.html (select-span)</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from src/app/app.component.html (select-span)">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="tag">&lt;div&gt;</span><span class="pln">
  Pick your favorite hero
  (</span><span class="tag">&lt;label&gt;&lt;input</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"checkbox"</span><span class="pln"> </span><span class="atn">checked</span><span class="pln"> (</span><span class="atn">change</span><span class="pln">)</span><span class="pun">=</span><span class="atv">"showSad = !showSad"</span><span class="tag">&gt;</span><span class="pln">show sad</span><span class="tag">&lt;/label&gt;</span><span class="pln">)
</span><span class="tag">&lt;/div&gt;</span><span class="pln">
</span><span class="tag">&lt;select</span><span class="pln"> [(</span><a href="api/forms/NgModel" class="code-anchor"><span class="atn">ngModel</span></a><span class="pln">)]</span><span class="pun">=</span><span class="atv">"hero"</span><span class="tag">&gt;</span><span class="pln">
  </span><span class="tag">&lt;span</span><span class="pln"> *</span><a href="api/common/NgForOf" class="code-anchor"><span class="atn">ngFor</span></a><span class="pun">=</span><span class="atv">"let h of heroes"</span><span class="tag">&gt;</span><span class="pln">
    </span><span class="tag">&lt;span</span><span class="pln"> *</span><a href="api/common/NgIf" class="code-anchor"><span class="atn">ngIf</span></a><span class="pun">=</span><span class="atv">"showSad || h.emotion !== 'sad'"</span><span class="tag">&gt;</span><span class="pln">
      </span><span class="tag">&lt;option</span><span class="pln"> [</span><span class="atn">ngValue</span><span class="pln">]</span><span class="pun">=</span><span class="atv">"h"</span><span class="tag">&gt;</span><span class="pln">{{h.name}} ({{h.emotion}})</span><span class="tag">&lt;/option&gt;</span><span class="pln">
    </span><span class="tag">&lt;/span&gt;</span><span class="pln">
  </span><span class="tag">&lt;/span&gt;</span><span class="pln">
</span><span class="tag">&lt;/select&gt;</span></code>
    </pre></aio-code></code-example><p translation-result="on">下拉列表就是空的。</p><p translation-origin="off">the drop down is empty.</p><div class="lightbox"><img src="generated/images/guide/structural-directives/bad-select.png" alt="spanned options don't work" width="290" height="36"></div><p translation-result="on">浏览器不会显示 <code>&lt;span&gt;</code> 中的 <code>&lt;option&gt;</code>。</p><p translation-origin="off">The browser won't display an <code>&lt;option&gt;</code> within a <code>&lt;span&gt;</code>.</p><h3 id="ng-container-to-the-rescue" translation-result="on">&lt;ng-container&gt; 的救赎<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#ng-container-to-the-rescue"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="ng-container-to-the-rescue">&lt;ng-container&gt; to the rescue<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#ng-container-to-the-rescue"><i class="material-icons">link</i></a></h3><p translation-result="on">Angular 的 <code>&lt;ng-container&gt;</code> 是一个分组元素，但它不会污染样式或元素布局，因为 Angular <em>压根不会把它放进 DOM</em> 中。</p><p translation-origin="off">The Angular <code>&lt;ng-container&gt;</code> is a grouping element that doesn't interfere with styles or layout because Angular <em>doesn't put it in the DOM</em>.</p><p translation-result="on">下面是重新实现的条件化段落，这次使用 <code>&lt;ng-container&gt;</code>。</p><p translation-origin="off">Here's the conditional paragraph again, this time using <code>&lt;ng-container&gt;</code>.</p><code-example path="structural-directives/src/app/app.component.html" header="src/app/app.component.html (ngif-ngcontainer)" region="ngif-ngcontainer" ng-version="9.0.0-rc.11"><div style="display:none">&lt;p&gt; I turned the corner &lt;ng-container *<a href="api/common/NgIf" class="code-anchor">ngIf</a>="hero"&gt; and saw {{hero.name}}. I waved &lt;/ng-container&gt; and continued on my way. &lt;/p&gt;</div><header class="ng-star-inserted">src/app/app.component.html (ngif-ngcontainer)</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from src/app/app.component.html (ngif-ngcontainer)">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="tag">&lt;p&gt;</span><span class="pln">
  I turned the corner
  </span><span class="tag">&lt;ng-container</span><span class="pln"> *</span><a href="api/common/NgIf" class="code-anchor"><span class="atn">ngIf</span></a><span class="pun">=</span><span class="atv">"hero"</span><span class="tag">&gt;</span><span class="pln">
    and saw {{hero.name}}. I waved
  </span><span class="tag">&lt;/ng-container&gt;</span><span class="pln">
  and continued on my way.
</span><span class="tag">&lt;/p&gt;</span></code>
    </pre></aio-code></code-example><p translation-result="on">这次就渲染对了。</p><p translation-origin="off">It renders properly.</p><div class="lightbox"><img src="generated/images/guide/structural-directives/good-paragraph.png" alt="ngcontainer paragraph with proper style" width="509" height="15"></div><p translation-result="on">现在用 <code>&lt;ng-container&gt;</code> 来根据条件排除选择框中的某个 <code>&lt;option&gt;</code>。</p><p translation-origin="off">Now conditionally exclude a <em>select</em> <code>&lt;option&gt;</code> with <code>&lt;ng-container&gt;</code>.</p><code-example path="structural-directives/src/app/app.component.html" header="src/app/app.component.html (select-ngcontainer)" region="select-ngcontainer" ng-version="9.0.0-rc.11"><div style="display:none">&lt;div&gt; Pick your favorite hero (&lt;label&gt;&lt;input type="checkbox" checked (change)="showSad = !showSad"&gt;show sad&lt;/label&gt;) &lt;/div&gt; &lt;select [(<a href="api/forms/NgModel" class="code-anchor">ngModel</a>)]="hero"&gt; &lt;ng-container *<a href="api/common/NgForOf" class="code-anchor">ngFor</a>="let h of heroes"&gt; &lt;ng-container *<a href="api/common/NgIf" class="code-anchor">ngIf</a>="showSad || h.emotion !== 'sad'"&gt; &lt;option [ngValue]="h"&gt;{{h.name}} ({{h.emotion}})&lt;/option&gt; &lt;/ng-container&gt; &lt;/ng-container&gt; &lt;/select&gt;</div><header class="ng-star-inserted">src/app/app.component.html (select-ngcontainer)</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from src/app/app.component.html (select-ngcontainer)">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="tag">&lt;div&gt;</span><span class="pln">
  Pick your favorite hero
  (</span><span class="tag">&lt;label&gt;&lt;input</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"checkbox"</span><span class="pln"> </span><span class="atn">checked</span><span class="pln"> (</span><span class="atn">change</span><span class="pln">)</span><span class="pun">=</span><span class="atv">"showSad = !showSad"</span><span class="tag">&gt;</span><span class="pln">show sad</span><span class="tag">&lt;/label&gt;</span><span class="pln">)
</span><span class="tag">&lt;/div&gt;</span><span class="pln">
</span><span class="tag">&lt;select</span><span class="pln"> [(</span><a href="api/forms/NgModel" class="code-anchor"><span class="atn">ngModel</span></a><span class="pln">)]</span><span class="pun">=</span><span class="atv">"hero"</span><span class="tag">&gt;</span><span class="pln">
  </span><span class="tag">&lt;ng-container</span><span class="pln"> *</span><a href="api/common/NgForOf" class="code-anchor"><span class="atn">ngFor</span></a><span class="pun">=</span><span class="atv">"let h of heroes"</span><span class="tag">&gt;</span><span class="pln">
    </span><span class="tag">&lt;ng-container</span><span class="pln"> *</span><a href="api/common/NgIf" class="code-anchor"><span class="atn">ngIf</span></a><span class="pun">=</span><span class="atv">"showSad || h.emotion !== 'sad'"</span><span class="tag">&gt;</span><span class="pln">
      </span><span class="tag">&lt;option</span><span class="pln"> [</span><span class="atn">ngValue</span><span class="pln">]</span><span class="pun">=</span><span class="atv">"h"</span><span class="tag">&gt;</span><span class="pln">{{h.name}} ({{h.emotion}})</span><span class="tag">&lt;/option&gt;</span><span class="pln">
    </span><span class="tag">&lt;/ng-container&gt;</span><span class="pln">
  </span><span class="tag">&lt;/ng-container&gt;</span><span class="pln">
</span><span class="tag">&lt;/select&gt;</span></code>
    </pre></aio-code></code-example><p translation-result="on">下拉框也工作正常。</p><p translation-origin="off">The drop down works properly.</p><div class="lightbox"><img src="generated/images/guide/structural-directives/select-ngcontainer-anim.gif" alt="ngcontainer options work properly" width="312" height="100"></div><div class="alert is-helpful"><p translation-result="on"><strong>注意：</strong> 记住，ngModel 指令是在 Angular 的 FormsModule 中定义的，你要在想使用它的模块的 <code>imports: [...]</code> 元数据中导入 FormsModule。</p><p translation-origin="off"><strong>Note:</strong> Remember that ngModel directive is defined as a part of Angular FormsModule and you need to include FormsModule in the imports: [...] section of the Angular module metadata, in which you want to use it.</p></div><p translation-result="on"><code>&lt;ng-container&gt;</code> 是一个由 Angular 解析器负责识别处理的语法元素。 它不是一个指令、组件、类或接口，更像是 JavaScript 中 <code>if</code> 块中的花括号。</p><p translation-origin="off">The <code>&lt;ng-container&gt;</code> is a syntax element recognized by the Angular parser. It's not a directive, component, class, or interface. It's more like the curly braces in a JavaScript <code>if</code>-block:</p><code-example language="javascript" ng-version="9.0.0-rc.11"><div style="display:none">if (someCondition) { statement1; statement2; statement3; }</div><aio-code><pre class="lang-javascript prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">someCondition</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  statement1</span><span class="pun">;</span><span class="pln">
  statement2</span><span class="pun">;</span><span class="pln">
  statement3</span><span class="pun">;</span><span class="pln">
</span><span class="pun">}</span></code>
    </pre></aio-code></code-example><p translation-result="on">没有这些花括号，JavaScript 只会执行第一句，而你原本的意图是把其中的所有语句都视为一体来根据条件执行。 而 <code>&lt;ng-container&gt;</code> 满足了 Angular 模板中类似的需求。</p><p translation-origin="off">Without those braces, JavaScript would only execute the first statement when you intend to conditionally execute all of them as a single block. The <code>&lt;ng-container&gt;</code> satisfies a similar need in Angular templates.</p><a id="unless"></a><h2 id="write-a-structural-directive" translation-result="on">写一个结构型指令<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#write-a-structural-directive"><i class="material-icons">link</i></a></h2><h2 translation-origin="off" id="write-a-structural-directive">Write a structural directive<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#write-a-structural-directive"><i class="material-icons">link</i></a></h2><p translation-result="on">在本节中，你会写一个名叫 <code>UnlessDirective</code> 的结构型指令，它是 <code><a href="api/common/NgIf" class="code-anchor">NgIf</a></code> 的反义词。 <code><a href="api/common/NgIf" class="code-anchor">NgIf</a></code> 在条件为 <code>true</code> 的时候显示模板内容，而 <code>UnlessDirective</code> 则会在条件为 <code>false</code> 时显示模板内容。</p><p translation-origin="off">In this section, you write an <code>UnlessDirective</code> structural directive that does the opposite of <code><a href="api/common/NgIf" class="code-anchor">NgIf</a></code>. <code><a href="api/common/NgIf" class="code-anchor">NgIf</a></code> displays the template content when the condition is <code>true</code>. <code>UnlessDirective</code> displays the content when the condition is <strong><em>false</em></strong>.</p><code-example path="structural-directives/src/app/app.component.html" header="src/app/app.component.html (appUnless-1)" region="appUnless-1" ng-version="9.0.0-rc.11"><div style="display:none">&lt;p *appUnless="condition"&gt;Show this sentence unless the condition is true.&lt;/p&gt;</div><header class="ng-star-inserted">src/app/app.component.html (appUnless-1)</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from src/app/app.component.html (appUnless-1)">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="tag">&lt;p</span><span class="pln"> *</span><span class="atn">appUnless</span><span class="pun">=</span><span class="atv">"condition"</span><span class="tag">&gt;</span><span class="pln">Show this sentence unless the condition is true.</span><span class="tag">&lt;/p&gt;</span></code>
    </pre></aio-code></code-example><p translation-result="on">创建指令很像创建组件。</p><p translation-origin="off">Creating a directive is similar to creating a component.</p><ul><li><p translation-result="on">导入 <code><a href="api/core/Directive" class="code-anchor">Directive</a></code> 装饰器（而不再是 <code><a href="api/core/Component" class="code-anchor">Component</a></code>）。</p><p translation-origin="off">Import the <code><a href="api/core/Directive" class="code-anchor">Directive</a></code> decorator (instead of the <code><a href="api/core/Component" class="code-anchor">Component</a></code> decorator).</p></li><li><p translation-result="on">导入符号 <code><a href="api/core/Input" class="code-anchor">Input</a></code>、<code><a href="api/core/TemplateRef" class="code-anchor">TemplateRef</a></code> 和 <code><a href="api/core/ViewContainerRef" class="code-anchor">ViewContainerRef</a></code>，你在<em>任何</em>结构型指令中都会需要它们。</p><p translation-origin="off">Import the <code><a href="api/core/Input" class="code-anchor">Input</a></code>, <code><a href="api/core/TemplateRef" class="code-anchor">TemplateRef</a></code>, and <code><a href="api/core/ViewContainerRef" class="code-anchor">ViewContainerRef</a></code> symbols; you'll need them for <em>any</em> structural directive.</p></li><li><p translation-result="on">给指令类添加装饰器。</p><p translation-origin="off">Apply the decorator to the directive class.</p></li><li><p translation-result="on">设置 CSS <em>属性选择器</em> ，以便在模板中标识出这个指令该应用于哪个元素。</p><p translation-origin="off">Set the CSS <em>attribute selector</em> that identifies the directive when applied to an element in a template.</p></li></ul><p translation-result="on">这里是起点：</p><p translation-origin="off">Here's how you might begin:</p><code-example path="structural-directives/src/app/unless.directive.ts" header="src/app/unless.directive.ts (skeleton)" region="skeleton" ng-version="9.0.0-rc.11"><div style="display:none">import { <a href="api/core/Directive" class="code-anchor">Directive</a>, <a href="api/core/Input" class="code-anchor">Input</a>, <a href="api/core/TemplateRef" class="code-anchor">TemplateRef</a>, <a href="api/core/ViewContainerRef" class="code-anchor">ViewContainerRef</a> } from '@angular/core'; @<a href="api/core/Directive" class="code-anchor">Directive</a>({ selector: '[appUnless]'}) export class UnlessDirective { }</div><header class="ng-star-inserted">src/app/unless.directive.ts (skeleton)</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from src/app/unless.directive.ts (skeleton)">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="kwd">import</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><a href="api/core/Directive" class="code-anchor"><span class="typ">Directive</span></a><span class="pun">,</span><span class="pln"> </span><a href="api/core/Input" class="code-anchor"><span class="typ">Input</span></a><span class="pun">,</span><span class="pln"> </span><a href="api/core/TemplateRef" class="code-anchor"><span class="typ">TemplateRef</span></a><span class="pun">,</span><span class="pln"> </span><a href="api/core/ViewContainerRef" class="code-anchor"><span class="typ">ViewContainerRef</span></a><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">from</span><span class="pln"> </span><span class="str">'@angular/core'</span><span class="pun">;</span><span class="pln">

</span><span class="lit">@</span><a href="api/core/Directive" class="code-anchor"><span class="lit">Directive</span></a><span class="pun">({</span><span class="pln"> selector</span><span class="pun">:</span><span class="pln"> </span><span class="str">'[appUnless]'</span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">UnlessDirective</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
</span><span class="pun">}</span></code>
    </pre></aio-code></code-example><p translation-result="on">指令的<em>选择器</em>通常是把指令的属性名括在方括号中，如 <code>[appUnless]</code>。 这个方括号定义出了一个 CSS <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors" title="MDN: Attribute selectors">属性选择器</a>。</p><p translation-origin="off">The directive's <em>selector</em> is typically the directive's <strong>attribute name</strong> in square brackets, <code>[appUnless]</code>. The brackets define a CSS <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors" title="MDN: Attribute selectors">attribute selector</a>.</p><p translation-result="on">该指令的<em>属性名</em>应该拼写成<em>小驼峰</em>形式，并且带有一个前缀。 但是，这个前缀不能用 <code>ng</code>，因为它只属于 Angular 本身。 请选择一些简短的，适合你自己或公司的前缀。 在这个例子中，前缀是 <code>app</code>。</p><p translation-origin="off">The directive <em>attribute name</em> should be spelled in <em>lowerCamelCase</em> and begin with a prefix. Don't use <code>ng</code>. That prefix belongs to Angular. Pick something short that fits you or your company. In this example, the prefix is <code>app</code>.</p><p translation-result="on">指令的<em>类名</em>用 <code><a href="api/core/Directive" class="code-anchor">Directive</a></code> 结尾，参见<a href="guide/styleguide#02-03" title="Angular 风格指南">风格指南</a>。 但 Angular 自己的指令例外。</p><p translation-origin="off">The directive <em>class</em> name ends in <code><a href="api/core/Directive" class="code-anchor">Directive</a></code> per the <a href="guide/styleguide#02-03" title="Angular Style Guide">style guide</a>. Angular's own directives do not.</p><h3 id="templateref-and-viewcontainerref" translation-result="on"><em>TemplateRef</em> 和 <em>ViewContainerRef</em><a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#templateref-and-viewcontainerref"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="templateref-and-viewcontainerref"><em>TemplateRef</em> and <em>ViewContainerRef</em><a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#templateref-and-viewcontainerref"><i class="material-icons">link</i></a></h3><p translation-result="on">像这个例子一样的简单结构型指令会从 Angular 生成的 <code>&lt;ng-template&gt;</code> 元素中创建一个<a href="api/core/EmbeddedViewRef" title="API: EmbeddedViewRef"><em>内嵌的视图</em></a>，并把这个视图插入到一个<a href="api/core/ViewContainerRef" title="API: ViewContainerRef"><em>视图容器</em></a>中，紧挨着本指令原来的宿主元素 <code>&lt;p&gt;</code>（译注：注意不是子节点，而是兄弟节点）。</p><p translation-origin="off">A simple structural directive like this one creates an <a href="api/core/EmbeddedViewRef" title="API: EmbeddedViewRef"><em>embedded view</em></a> from the Angular-generated <code>&lt;ng-template&gt;</code> and inserts that view in a <a href="api/core/ViewContainerRef" title="API: ViewContainerRef"><em>view container</em></a> adjacent to the directive's original <code>&lt;p&gt;</code> host element.</p><p translation-result="on">你可以使用<a href="api/core/TemplateRef" title="API: TemplateRef"><code>TemplateRef</code></a>取得 <code>&lt;ng-template&gt;</code> 的内容，并通过<a href="api/core/ViewContainerRef" title="API: ViewContainerRef"><code>ViewContainerRef</code></a>来访问这个<em>视图容器</em>。</p><p translation-origin="off">You'll acquire the <code>&lt;ng-template&gt;</code> contents with a <a href="api/core/TemplateRef" title="API: TemplateRef"><code>TemplateRef</code></a>and access the <em>view container</em> through a <a href="api/core/ViewContainerRef" title="API: ViewContainerRef"><code>ViewContainerRef</code></a>.</p><p translation-result="on">你可以把它们都注入到指令的构造函数中，作为该类的私有属性。</p><p translation-origin="off">You inject both in the directive constructor as private variables of the class.</p><code-example path="structural-directives/src/app/unless.directive.ts" header="src/app/unless.directive.ts (ctor)" region="ctor" ng-version="9.0.0-rc.11"><div style="display:none">constructor( private templateRef: <a href="api/core/TemplateRef" class="code-anchor">TemplateRef</a>&lt;any&gt;, private viewContainer: <a href="api/core/ViewContainerRef" class="code-anchor">ViewContainerRef</a>) { }</div><header class="ng-star-inserted">src/app/unless.directive.ts (ctor)</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from src/app/unless.directive.ts (ctor)">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="kwd">constructor</span><span class="pun">(</span><span class="pln">
  </span><span class="kwd">private</span><span class="pln"> templateRef</span><span class="pun">:</span><span class="pln"> </span><a href="api/core/TemplateRef" class="code-anchor"><span class="typ">TemplateRef</span></a><span class="str">&lt;any&gt;</span><span class="pun">,</span><span class="pln">
  </span><span class="kwd">private</span><span class="pln"> viewContainer</span><span class="pun">:</span><span class="pln"> </span><a href="api/core/ViewContainerRef" class="code-anchor"><span class="typ">ViewContainerRef</span></a><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="pun">}</span></code>
    </pre></aio-code></code-example><h3 id="the-appunless-property" translation-result="on"><em>appUnless</em> 属性<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#the-appunless-property"><i class="material-icons">link</i></a></h3><h3 translation-origin="off" id="the-appunless-property">The <em>appUnless</em> property<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#the-appunless-property"><i class="material-icons">link</i></a></h3><p translation-result="on">该指令的使用者会把一个 true/false 条件绑定到 <code>[appUnless]</code> 属性上。 也就是说，该指令需要一个带有 <code>@<a href="api/core/Input" class="code-anchor">Input</a></code> 的 <code>appUnless</code> 属性。</p><p translation-origin="off">The directive consumer expects to bind a true/false condition to <code>[appUnless]</code>. That means the directive needs an <code>appUnless</code> property, decorated with <code>@<a href="api/core/Input" class="code-anchor">Input</a></code></p><div class="alert is-helpful"><p translation-result="on">要了解关于 <code>@<a href="api/core/Input" class="code-anchor">Input</a></code> 的更多知识，参见<a href="guide/template-syntax#inputs-outputs"><em>模板语法</em></a>一章。</p><p translation-origin="off">Read about <code>@<a href="api/core/Input" class="code-anchor">Input</a></code> in the <a href="guide/template-syntax#inputs-outputs"><em>Template Syntax</em></a> guide.</p></div><code-example path="structural-directives/src/app/unless.directive.ts" header="src/app/unless.directive.ts (set)" region="set" ng-version="9.0.0-rc.11"><div style="display:none">@<a href="api/core/Input" class="code-anchor">Input</a>() set appUnless(condition: boolean) { if (!condition &amp;&amp; !this.hasView) { this.viewContainer.createEmbeddedView(this.templateRef); this.hasView = true; } else if (condition &amp;&amp; this.hasView) { this.viewContainer.clear(); this.hasView = false; } }</div><header class="ng-star-inserted">src/app/unless.directive.ts (set)</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from src/app/unless.directive.ts (set)">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="lit">@</span><a href="api/core/Input" class="code-anchor"><span class="lit">Input</span></a><span class="pun">()</span><span class="pln"> </span><span class="kwd">set</span><span class="pln"> appUnless</span><span class="pun">(</span><span class="pln">condition</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">boolean</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(!</span><span class="pln">condition </span><span class="pun">&amp;&amp;</span><span class="pln"> </span><span class="pun">!</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">hasView</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">viewContainer</span><span class="pun">.</span><span class="pln">createEmbeddedView</span><span class="pun">(</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">templateRef</span><span class="pun">);</span><span class="pln">
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">hasView </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">true</span><span class="pun">;</span><span class="pln">
  </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">else</span><span class="pln"> </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">condition </span><span class="pun">&amp;&amp;</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">hasView</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">viewContainer</span><span class="pun">.</span><span class="pln">clear</span><span class="pun">();</span><span class="pln">
    </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">hasView </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">false</span><span class="pun">;</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span></code>
    </pre></aio-code></code-example><p translation-result="on">一旦该值的条件发生了变化，Angular 就会去设置 <code>appUnless</code> 属性。因为不能用 <code>appUnless</code> 属性，所以你要为它定义一个设置器（setter）。</p><p translation-origin="off">Angular sets the <code>appUnless</code> property whenever the value of the condition changes. Because the <code>appUnless</code> property does work, it needs a setter.</p><ul><li><p translation-result="on">如果条件为假，并且以前尚未创建过该视图，就告诉<em>视图容器（ViewContainer）</em>根据模板创建一个<em>内嵌视图</em>。</p><p translation-origin="off">If the condition is falsy and the view hasn't been created previously, tell the <em>view container</em> to create the <em>embedded view</em> from the template.</p></li><li><p translation-result="on">如果条件为真，并且视图已经显示出来了，就会清除该容器，并销毁该视图。</p><p translation-origin="off">If the condition is truthy and the view is currently displayed, clear the container which also destroys the view.</p></li></ul><p translation-result="on">没有人会读取 <code>appUnless</code> 属性，因此它不需要定义 getter。</p><p translation-origin="off">Nobody reads the <code>appUnless</code> property so it doesn't need a getter.</p><p translation-result="on">完整的指令代码如下：</p><p translation-origin="off">The completed directive code looks like this:</p><code-example path="structural-directives/src/app/unless.directive.ts" header="src/app/unless.directive.ts (excerpt)" region="no-docs" ng-version="9.0.0-rc.11"><div style="display:none">import { <a href="api/core/Directive" class="code-anchor">Directive</a>, <a href="api/core/Input" class="code-anchor">Input</a>, <a href="api/core/TemplateRef" class="code-anchor">TemplateRef</a>, <a href="api/core/ViewContainerRef" class="code-anchor">ViewContainerRef</a> } from '@angular/core'; /** * Add the template content to the DOM unless the condition is true. */ @<a href="api/core/Directive" class="code-anchor">Directive</a>({ selector: '[appUnless]'}) export class UnlessDirective { private hasView = false; constructor( private templateRef: <a href="api/core/TemplateRef" class="code-anchor">TemplateRef</a>&lt;any&gt;, private viewContainer: <a href="api/core/ViewContainerRef" class="code-anchor">ViewContainerRef</a>) { } @<a href="api/core/Input" class="code-anchor">Input</a>() set appUnless(condition: boolean) { if (!condition &amp;&amp; !this.hasView) { this.viewContainer.createEmbeddedView(this.templateRef); this.hasView = true; } else if (condition &amp;&amp; this.hasView) { this.viewContainer.clear(); this.hasView = false; } } }</div><header class="ng-star-inserted">src/app/unless.directive.ts (excerpt)</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from src/app/unless.directive.ts (excerpt)">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="kwd">import</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><a href="api/core/Directive" class="code-anchor"><span class="typ">Directive</span></a><span class="pun">,</span><span class="pln"> </span><a href="api/core/Input" class="code-anchor"><span class="typ">Input</span></a><span class="pun">,</span><span class="pln"> </span><a href="api/core/TemplateRef" class="code-anchor"><span class="typ">TemplateRef</span></a><span class="pun">,</span><span class="pln"> </span><a href="api/core/ViewContainerRef" class="code-anchor"><span class="typ">ViewContainerRef</span></a><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">from</span><span class="pln"> </span><span class="str">'@angular/core'</span><span class="pun">;</span><span class="pln">

</span><span class="com">/**
 * Add the template content to the DOM unless the condition is true.
 */</span><span class="pln">
</span><span class="lit">@</span><a href="api/core/Directive" class="code-anchor"><span class="lit">Directive</span></a><span class="pun">({</span><span class="pln"> selector</span><span class="pun">:</span><span class="pln"> </span><span class="str">'[appUnless]'</span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">UnlessDirective</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  </span><span class="kwd">private</span><span class="pln"> hasView </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">false</span><span class="pun">;</span><span class="pln">

  </span><span class="kwd">constructor</span><span class="pun">(</span><span class="pln">
    </span><span class="kwd">private</span><span class="pln"> templateRef</span><span class="pun">:</span><span class="pln"> </span><a href="api/core/TemplateRef" class="code-anchor"><span class="typ">TemplateRef</span></a><span class="str">&lt;any&gt;</span><span class="pun">,</span><span class="pln">
    </span><span class="kwd">private</span><span class="pln"> viewContainer</span><span class="pun">:</span><span class="pln"> </span><a href="api/core/ViewContainerRef" class="code-anchor"><span class="typ">ViewContainerRef</span></a><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="pun">}</span><span class="pln">

  </span><span class="lit">@</span><a href="api/core/Input" class="code-anchor"><span class="lit">Input</span></a><span class="pun">()</span><span class="pln"> </span><span class="kwd">set</span><span class="pln"> appUnless</span><span class="pun">(</span><span class="pln">condition</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">boolean</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(!</span><span class="pln">condition </span><span class="pun">&amp;&amp;</span><span class="pln"> </span><span class="pun">!</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">hasView</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
      </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">viewContainer</span><span class="pun">.</span><span class="pln">createEmbeddedView</span><span class="pun">(</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">templateRef</span><span class="pun">);</span><span class="pln">
      </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">hasView </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">true</span><span class="pun">;</span><span class="pln">
    </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">else</span><span class="pln"> </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">condition </span><span class="pun">&amp;&amp;</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">hasView</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
      </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">viewContainer</span><span class="pun">.</span><span class="pln">clear</span><span class="pun">();</span><span class="pln">
      </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">hasView </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">false</span><span class="pun">;</span><span class="pln">
    </span><span class="pun">}</span><span class="pln">
  </span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span></code>
    </pre></aio-code></code-example><p translation-result="on">把这个指令添加到 AppModule 的 <code>declarations</code> 数组中。</p><p translation-origin="off">Add this directive to the <code>declarations</code> array of the AppModule.</p><p translation-result="on">然后创建一些 HTML 来试用一下。</p><p translation-origin="off">Then create some HTML to try it.</p><code-example path="structural-directives/src/app/app.component.html" header="src/app/app.component.html (appUnless)" region="appUnless" ng-version="9.0.0-rc.11"><div style="display:none">&lt;p *appUnless="condition" class="unless a"&gt; (A) This paragraph is displayed because the condition is false. &lt;/p&gt; &lt;p *appUnless="!condition" class="unless b"&gt; (B) Although the condition is true, this paragraph is displayed because appUnless is set to false. &lt;/p&gt;</div><header class="ng-star-inserted">src/app/app.component.html (appUnless)</header><aio-code class="headed-code"><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from src/app/app.component.html (appUnless)">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="tag">&lt;p</span><span class="pln"> *</span><span class="atn">appUnless</span><span class="pun">=</span><span class="atv">"condition"</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"unless a"</span><span class="tag">&gt;</span><span class="pln">
  (A) This paragraph is displayed because the condition is false.
</span><span class="tag">&lt;/p&gt;</span><span class="pln">

</span><span class="tag">&lt;p</span><span class="pln"> *</span><span class="atn">appUnless</span><span class="pun">=</span><span class="atv">"!condition"</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"unless b"</span><span class="tag">&gt;</span><span class="pln">
  (B) Although the condition is true,
  this paragraph is displayed because appUnless is set to false.
</span><span class="tag">&lt;/p&gt;</span></code>
    </pre></aio-code></code-example><p translation-result="on">当 <code>condition</code> 为 <code>false</code> 时，顶部的段落就会显示出来，而底部的段落消失了。 当 <code>condition</code> 为 <code>true</code> 时，顶部的段落被移除了，而底部的段落显示了出来。</p><p translation-origin="off">When the <code>condition</code> is falsy, the top (A) paragraph appears and the bottom (B) paragraph disappears. When the <code>condition</code> is truthy, the top (A) paragraph is removed and the bottom (B) paragraph appears.</p><div class="lightbox"><img src="generated/images/guide/structural-directives/unless-anim.gif" alt="UnlessDirective in action" width="524" height="100"></div><a id="summary"></a><h2 id="summary" translation-result="on">小结<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#summary"><i class="material-icons">link</i></a></h2><h2 translation-origin="off" id="summary">Summary<a title="Link to this heading" class="header-link" aria-hidden="true" href="guide/structural-directives#summary"><i class="material-icons">link</i></a></h2><p translation-result="on">你可以去<live-example ng-version="9.0.0-rc.11"><span style="display:none"></span><span><span class="ng-star-inserted"><a target="_blank" title="现场演练" href="generated/live-examples/structural-directives/stackblitz.html">现场演练</a><span class="ng-star-inserted"> / <a download="" title="下载范例" href="generated/zips/structural-directives/structural-directives.zip">下载范例</a></span></span></span></live-example>中下载本章的源码。</p><p translation-origin="off">You can both try and download the source code for this guide in the<live-example ng-version="9.0.0-rc.11"><span style="display:none"></span><span><span class="ng-star-inserted"><a target="_blank" title="现场演练" href="generated/live-examples/structural-directives/stackblitz.html">现场演练</a><span class="ng-star-inserted"> / <a download="" title="下载范例" href="generated/zips/structural-directives/structural-directives.zip">下载范例</a></span></span></span></live-example>.</p><p translation-result="on">本章相关的代码如下：</p><p translation-origin="off">Here is the source from the <code>src/app/</code> folder.</p><code-tabs ng-version="9.0.0-rc.11"><div style="display:none"><code-pane header="app.component.ts" path="structural-directives/src/app/app.component.ts">import { <a href="api/core/Component" class="code-anchor">Component</a> } from '@angular/core'; import { Hero, heroes } from './hero'; @<a href="api/core/Component" class="code-anchor">Component</a>({ selector: 'app-root', <a href="api/core/Component#templateUrl" class="code-anchor">templateUrl</a>: './app.component.html', <a href="api/core/Component#styleUrls" class="code-anchor">styleUrls</a>: [ './app.component.css' ] }) export class AppComponent { heroes = heroes; hero = this.heroes[0]; condition = false; logs: string[] = []; showSad = true; status = 'ready'; <a href="api/core/IterableChangeRecord#trackById" class="code-anchor">trackById</a>(index: number, hero: Hero): number { return hero.id; } }</code-pane><code-pane header="app.component.html" path="structural-directives/src/app/app.component.html">&lt;h1&gt;Structural Directives&lt;/h1&gt; &lt;p&gt;Conditional display of hero&lt;/p&gt; &lt;blockquote&gt; &lt;div *<a href="api/common/NgIf" class="code-anchor">ngIf</a>="hero" class="name"&gt;{{hero.name}}&lt;/div&gt; &lt;/blockquote&gt; &lt;p&gt;<a href="api/common/NumberSymbol#List" class="code-anchor">List</a> of heroes&lt;/p&gt; &lt;ul&gt; &lt;li *<a href="api/common/NgForOf" class="code-anchor">ngFor</a>="let hero of heroes"&gt;{{hero.name}}&lt;/li&gt; &lt;/ul&gt; &lt;hr&gt; &lt;h2 id="<a href="api/common/NgIf" class="code-anchor">ngIf</a>"&gt;<a href="api/common/NgIf" class="code-anchor">NgIf</a>&lt;/h2&gt; &lt;p *<a href="api/common/NgIf" class="code-anchor">ngIf</a>="true"&gt; Expression is true and <a href="api/common/NgIf" class="code-anchor">ngIf</a> is true. This paragraph is in the DOM. &lt;/p&gt; &lt;p *<a href="api/common/NgIf" class="code-anchor">ngIf</a>="false"&gt; Expression is false and <a href="api/common/NgIf" class="code-anchor">ngIf</a> is false. This paragraph is not in the DOM. &lt;/p&gt; &lt;p [style.display]="'block'"&gt; Expression sets display to "block". This paragraph is visible. &lt;/p&gt; &lt;p [style.display]="'none'"&gt; Expression sets display to "none". This paragraph is hidden but still in the DOM. &lt;/p&gt; &lt;h4&gt;<a href="api/common/NgIf" class="code-anchor">NgIf</a> with template&lt;/h4&gt; &lt;p&gt;&amp;lt;ng-template&amp;gt; element&lt;/p&gt; &lt;ng-template [<a href="api/common/NgIf" class="code-anchor">ngIf</a>]="hero"&gt; &lt;div class="name"&gt;{{hero.name}}&lt;/div&gt; &lt;/ng-template&gt; &lt;hr&gt; &lt;h2 id="ng-container"&gt;&amp;lt;ng-container&amp;gt;&lt;/h2&gt; &lt;h4&gt;*<a href="api/common/NgIf" class="code-anchor">ngIf</a> with a &amp;lt;ng-container&amp;gt;&lt;/h4&gt; &lt;button (click)="hero = hero ? null : heroes[0]"&gt;Toggle hero&lt;/button&gt; &lt;p&gt; I turned the corner &lt;ng-container *<a href="api/common/NgIf" class="code-anchor">ngIf</a>="hero"&gt; and saw {{hero.name}}. I waved &lt;/ng-container&gt; and continued on my way. &lt;/p&gt; &lt;p&gt; I turned the corner &lt;span *<a href="api/common/NgIf" class="code-anchor">ngIf</a>="hero"&gt; and saw {{hero.name}}. I waved &lt;/span&gt; and continued on my way. &lt;/p&gt; &lt;p&gt;&lt;i&gt;&amp;lt;select&amp;gt; with &amp;lt;span&amp;gt;&lt;/i&gt;&lt;/p&gt; &lt;div&gt; Pick your favorite hero (&lt;label&gt;&lt;input type="checkbox" checked (change)="showSad = !showSad"&gt;show sad&lt;/label&gt;) &lt;/div&gt; &lt;select [(<a href="api/forms/NgModel" class="code-anchor">ngModel</a>)]="hero"&gt; &lt;span *<a href="api/common/NgForOf" class="code-anchor">ngFor</a>="let h of heroes"&gt; &lt;span *<a href="api/common/NgIf" class="code-anchor">ngIf</a>="showSad || h.emotion !== 'sad'"&gt; &lt;option [ngValue]="h"&gt;{{h.name}} ({{h.emotion}})&lt;/option&gt; &lt;/span&gt; &lt;/span&gt; &lt;/select&gt; &lt;p&gt;&lt;i&gt;&amp;lt;select&amp;gt; with &amp;lt;ng-container&amp;gt;&lt;/i&gt;&lt;/p&gt; &lt;div&gt; Pick your favorite hero (&lt;label&gt;&lt;input type="checkbox" checked (change)="showSad = !showSad"&gt;show sad&lt;/label&gt;) &lt;/div&gt; &lt;select [(<a href="api/forms/NgModel" class="code-anchor">ngModel</a>)]="hero"&gt; &lt;ng-container *<a href="api/common/NgForOf" class="code-anchor">ngFor</a>="let h of heroes"&gt; &lt;ng-container *<a href="api/common/NgIf" class="code-anchor">ngIf</a>="showSad || h.emotion !== 'sad'"&gt; &lt;option [ngValue]="h"&gt;{{h.name}} ({{h.emotion}})&lt;/option&gt; &lt;/ng-container&gt; &lt;/ng-container&gt; &lt;/select&gt; &lt;br&gt;&lt;br&gt; &lt;hr&gt; &lt;h2 id="<a href="api/common/NgForOf" class="code-anchor">ngFor</a>"&gt;NgFor&lt;/h2&gt; &lt;div class="box"&gt; &lt;p class="code"&gt;&amp;lt;div *<a href="api/common/NgForOf" class="code-anchor">ngFor</a>="let hero of heroes; let i=index; let <a href="api/common/NgForOfContext#odd" class="code-anchor">odd</a>=<a href="api/common/NgForOfContext#odd" class="code-anchor">odd</a>; trackBy: <a href="api/core/IterableChangeRecord#trackById" class="code-anchor">trackById</a>" [class.odd]="<a href="api/common/NgForOfContext#odd" class="code-anchor">odd</a>"&amp;gt;&lt;/p&gt; &lt;div *<a href="api/common/NgForOf" class="code-anchor">ngFor</a>="let hero of heroes; let i=index; let <a href="api/common/NgForOfContext#odd" class="code-anchor">odd</a>=<a href="api/common/NgForOfContext#odd" class="code-anchor">odd</a>; trackBy: <a href="api/core/IterableChangeRecord#trackById" class="code-anchor">trackById</a>" [class.odd]="<a href="api/common/NgForOfContext#odd" class="code-anchor">odd</a>"&gt; ({{i}}) {{hero.name}} &lt;/div&gt; &lt;p class="code"&gt;&amp;lt;ng-template <a href="api/common/NgForOf" class="code-anchor">ngFor</a> let-hero [<a href="api/common/NgForOf" class="code-anchor">ngForOf</a>]="heroes" let-i="index" let-odd="<a href="api/common/NgForOfContext#odd" class="code-anchor">odd</a>" [<a href="api/common/NgForOf#ngForTrackBy" class="code-anchor">ngForTrackBy</a>]="<a href="api/core/IterableChangeRecord#trackById" class="code-anchor">trackById</a>"/&amp;gt;&lt;/p&gt; &lt;ng-template <a href="api/common/NgForOf" class="code-anchor">ngFor</a> let-hero [<a href="api/common/NgForOf" class="code-anchor">ngForOf</a>]="heroes" let-i="index" let-odd="<a href="api/common/NgForOfContext#odd" class="code-anchor">odd</a>" [<a href="api/common/NgForOf#ngForTrackBy" class="code-anchor">ngForTrackBy</a>]="<a href="api/core/IterableChangeRecord#trackById" class="code-anchor">trackById</a>"&gt; &lt;div [class.odd]="<a href="api/common/NgForOfContext#odd" class="code-anchor">odd</a>"&gt;({{i}}) {{hero.name}}&lt;/div&gt; &lt;/ng-template&gt; &lt;/div&gt; &lt;hr&gt; &lt;h2 id="<a href="api/common/NgSwitch" class="code-anchor">ngSwitch</a>"&gt;<a href="api/common/NgSwitch" class="code-anchor">NgSwitch</a>&lt;/h2&gt; &lt;div&gt;Pick your favorite hero&lt;/div&gt; &lt;p&gt; &lt;label *<a href="api/common/NgForOf" class="code-anchor">ngFor</a>="let h of heroes"&gt; &lt;input type="radio" name="heroes" [(<a href="api/forms/NgModel" class="code-anchor">ngModel</a>)]="hero" [value]="h"&gt;{{h.name}} &lt;/label&gt; &lt;label&gt;&lt;input type="radio" name="heroes" (click)="hero = null"&gt;None of the above&lt;/label&gt; &lt;/p&gt; &lt;h4&gt;<a href="api/common/NgSwitch" class="code-anchor">NgSwitch</a>&lt;/h4&gt; &lt;div [<a href="api/common/NgSwitch" class="code-anchor">ngSwitch</a>]="hero?.emotion"&gt; &lt;app-happy-hero *<a href="api/common/NgSwitchCase" class="code-anchor">ngSwitchCase</a>="'happy'" [hero]="hero"&gt;&lt;/app-happy-hero&gt; &lt;app-sad-hero *<a href="api/common/NgSwitchCase" class="code-anchor">ngSwitchCase</a>="'sad'" [hero]="hero"&gt;&lt;/app-sad-hero&gt; &lt;app-confused-hero *<a href="api/common/NgSwitchCase" class="code-anchor">ngSwitchCase</a>="'confused'" [hero]="hero"&gt;&lt;/app-confused-hero&gt; &lt;app-unknown-hero *<a href="api/common/NgSwitchDefault" class="code-anchor">ngSwitchDefault</a> [hero]="hero"&gt;&lt;/app-unknown-hero&gt; &lt;/div&gt; &lt;h4&gt;<a href="api/common/NgSwitch" class="code-anchor">NgSwitch</a> with &amp;lt;ng-template&amp;gt;&lt;/h4&gt; &lt;div [<a href="api/common/NgSwitch" class="code-anchor">ngSwitch</a>]="hero?.emotion"&gt; &lt;ng-template [<a href="api/common/NgSwitchCase" class="code-anchor">ngSwitchCase</a>]="'happy'"&gt; &lt;app-happy-hero [hero]="hero"&gt;&lt;/app-happy-hero&gt; &lt;/ng-template&gt; &lt;ng-template [<a href="api/common/NgSwitchCase" class="code-anchor">ngSwitchCase</a>]="'sad'"&gt; &lt;app-sad-hero [hero]="hero"&gt;&lt;/app-sad-hero&gt; &lt;/ng-template&gt; &lt;ng-template [<a href="api/common/NgSwitchCase" class="code-anchor">ngSwitchCase</a>]="'confused'"&gt; &lt;app-confused-hero [hero]="hero"&gt;&lt;/app-confused-hero&gt; &lt;/ng-template &gt; &lt;ng-template <a href="api/common/NgSwitchDefault" class="code-anchor">ngSwitchDefault</a>&gt; &lt;app-unknown-hero [hero]="hero"&gt;&lt;/app-unknown-hero&gt; &lt;/ng-template&gt; &lt;/div&gt; &lt;hr&gt; &lt;h2&gt;&amp;lt;ng-template&amp;gt;&lt;/h2&gt; &lt;p&gt;Hip!&lt;/p&gt; &lt;ng-template&gt; &lt;p&gt;Hip!&lt;/p&gt; &lt;/ng-template&gt; &lt;p&gt;Hooray!&lt;/p&gt; &lt;hr&gt; &lt;h2 id="appUnless"&gt;UnlessDirective&lt;/h2&gt; &lt;p&gt; The condition is currently &lt;span [<a href="api/common/NgClass" class="code-anchor">ngClass</a>]="{ 'a': !condition, 'b': condition, 'unless': true }"&gt;{{condition}}&lt;/span&gt;. &lt;button (click)="condition = !condition" [<a href="api/common/NgClass" class="code-anchor">ngClass</a>] = "{ 'a': condition, 'b': !condition }" &gt; Toggle condition to {{condition ? 'false' : 'true'}} &lt;/button&gt; &lt;/p&gt; &lt;p *appUnless="condition" class="unless a"&gt; (A) This paragraph is displayed because the condition is false. &lt;/p&gt; &lt;p *appUnless="!condition" class="unless b"&gt; (B) Although the condition is true, this paragraph is displayed because appUnless is set to false. &lt;/p&gt; &lt;h4&gt;UnlessDirective with template&lt;/h4&gt; &lt;p *appUnless="condition"&gt;Show this sentence unless the condition is true.&lt;/p&gt; &lt;p *appUnless="condition" class="code unless"&gt; (A) &amp;lt;p *appUnless="condition" class="code unless"&amp;gt; &lt;/p&gt; &lt;ng-template [appUnless]="condition"&gt; &lt;p class="code unless"&gt; (A) &amp;lt;ng-template [appUnless]="condition"&amp;gt; &lt;/p&gt; &lt;/ng-template&gt;</code-pane><code-pane header="app.component.css" path="structural-directives/src/app/app.component.css">button { min-width: 100px; font-size: 100%; } .box { border: 1px solid gray; max-width: 600px; padding: 4px; } .choices { font-style: italic; } code, .code { background-color: #eee; color: black; font-family: Courier, sans-serif; font-size: 85%; } div.code { width: 400px; } .heroic { font-size: 150%; font-weight: bold; } hr { margin: 40px 0 } .odd { background-color: palegoldenrod; } td, th { text-align: left; vertical-align: top; } p span { color: red; font-size: 70%; } .unless { border: 2px solid; padding: 6px; } p.unless { width: 500px; } button.a, span.a, .unless.a { color: red; border-color: gold; background-color: yellow; font-size: 100%; } button.b, span.b, .unless.b { color: black; border-color: green; background-color: lightgreen; font-size: 100%; }</code-pane><code-pane header="app.module.ts" path="structural-directives/src/app/app.module.ts">import { <a href="api/core/NgModule" class="code-anchor">NgModule</a> } from '@angular/core'; import { <a href="api/forms/FormsModule" class="code-anchor">FormsModule</a> } from '@angular/forms'; import { <a href="api/platform-browser/BrowserModule" class="code-anchor">BrowserModule</a> } from '@angular/platform-browser'; import { AppComponent } from './app.component'; import { heroSwitchComponents } from './hero-switch.components'; import { UnlessDirective } from './unless.directive'; @<a href="api/core/NgModule" class="code-anchor">NgModule</a>({ imports: [ <a href="api/platform-browser/BrowserModule" class="code-anchor">BrowserModule</a>, <a href="api/forms/FormsModule" class="code-anchor">FormsModule</a> ], declarations: [ AppComponent, heroSwitchComponents, UnlessDirective ], bootstrap: [ AppComponent ] }) export class AppModule { }</code-pane><code-pane header="hero.ts" path="structural-directives/src/app/hero.ts">export interface Hero { id: number; name: string; emotion?: string; } export const heroes: Hero[] = [ { id: 1, name: 'Dr Nice', emotion: 'happy'}, { id: 2, name: 'Narco', emotion: 'sad' }, { id: 3, name: 'Windstorm', emotion: 'confused' }, { id: 4, name: 'Magneta'} ];</code-pane><code-pane header="hero-switch.components.ts" path="structural-directives/src/app/hero-switch.components.ts">import { <a href="api/core/Component" class="code-anchor">Component</a>, <a href="api/core/Input" class="code-anchor">Input</a> } from '@angular/core'; import { Hero } from './hero'; @<a href="api/core/Component" class="code-anchor">Component</a>({ selector: 'app-happy-hero', template: `Wow. You like {{hero.name}}. What a happy hero ... just like you.` }) export class HappyHeroComponent { @<a href="api/core/Input" class="code-anchor">Input</a>() hero: Hero; } @<a href="api/core/Component" class="code-anchor">Component</a>({ selector: 'app-sad-hero', template: `You like {{hero.name}}? Such a sad hero. Are you sad too?` }) export class SadHeroComponent { @<a href="api/core/Input" class="code-anchor">Input</a>() hero: Hero; } @<a href="api/core/Component" class="code-anchor">Component</a>({ selector: 'app-confused-hero', template: `Are you as confused as {{hero.name}}?` }) export class ConfusedHeroComponent { @<a href="api/core/Input" class="code-anchor">Input</a>() hero: Hero; } @<a href="api/core/Component" class="code-anchor">Component</a>({ selector: 'app-unknown-hero', template: `{{<a href="api/common/http/HttpErrorResponse#message" class="code-anchor">message</a>}}` }) export class UnknownHeroComponent { @<a href="api/core/Input" class="code-anchor">Input</a>() hero: Hero; get <a href="api/common/http/HttpErrorResponse#message" class="code-anchor">message</a>() { return this.hero &amp;&amp; this.hero.name ? `${this.hero.name} is strange and mysterious.` : 'Are you feeling indecisive?'; } } export const heroSwitchComponents = [ HappyHeroComponent, SadHeroComponent, ConfusedHeroComponent, UnknownHeroComponent ];</code-pane><code-pane header="unless.directive.ts" path="structural-directives/src/app/unless.directive.ts">import { <a href="api/core/Directive" class="code-anchor">Directive</a>, <a href="api/core/Input" class="code-anchor">Input</a>, <a href="api/core/TemplateRef" class="code-anchor">TemplateRef</a>, <a href="api/core/ViewContainerRef" class="code-anchor">ViewContainerRef</a> } from '@angular/core'; /** * Add the template content to the DOM unless the condition is true. * * If the expression assigned to `appUnless` evaluates to a truthy value * then the templated elements are removed removed from the DOM, * the templated elements are (re)inserted into the DOM. * * &lt;div *appUnless="errorCount" class="success"&gt; * Congrats! Everything is great! * &lt;/div&gt; * * ### Syntax * * - `&lt;div *appUnless="condition"&gt;...&lt;/div&gt;` * - `&lt;ng-template [appUnless]="condition"&gt;&lt;div&gt;...&lt;/div&gt;&lt;/ng-template&gt;` * */ @<a href="api/core/Directive" class="code-anchor">Directive</a>({ selector: '[appUnless]'}) export class UnlessDirective { private hasView = false; constructor( private templateRef: <a href="api/core/TemplateRef" class="code-anchor">TemplateRef</a>&lt;any&gt;, private viewContainer: <a href="api/core/ViewContainerRef" class="code-anchor">ViewContainerRef</a>) { } @<a href="api/core/Input" class="code-anchor">Input</a>() set appUnless(condition: boolean) { if (!condition &amp;&amp; !this.hasView) { this.viewContainer.createEmbeddedView(this.templateRef); this.hasView = true; } else if (condition &amp;&amp; this.hasView) { this.viewContainer.clear(); this.hasView = false; } } }</code-pane></div><mat-card class="mat-card"><mat-tab-group class="mat-tab-group code-tab-group mat-primary"><mat-tab-header class="mat-tab-header mat-tab-header-pagination-controls-enabled"><div aria-hidden="true" mat-ripple="" class="mat-ripple mat-tab-header-pagination mat-tab-header-pagination-before mat-elevation-z4 mat-tab-header-pagination-disabled"><div class="mat-tab-header-pagination-chevron"></div></div><div class="mat-tab-label-container"><div role="tablist" class="mat-tab-list" style="transform:translateX(0)"><div class="mat-tab-labels"><div role="tab" mattablabelwrapper="" mat-ripple="" cdkmonitorelementfocus="" class="mat-ripple mat-tab-label mat-tab-label-active ng-star-inserted" id="mat-tab-label-0-0" tabindex="0" aria-posinset="1" aria-setsize="7" aria-controls="mat-tab-content-0-0" aria-selected="true" aria-disabled="false"><div class="mat-tab-label-content"><span class="ng-star-inserted">app.component.ts</span></div></div><div role="tab" mattablabelwrapper="" mat-ripple="" cdkmonitorelementfocus="" class="mat-ripple mat-tab-label ng-star-inserted" id="mat-tab-label-0-1" tabindex="-1" aria-posinset="2" aria-setsize="7" aria-controls="mat-tab-content-0-1" aria-selected="false" aria-disabled="false"><div class="mat-tab-label-content"><span class="ng-star-inserted">app.component.html</span></div></div><div role="tab" mattablabelwrapper="" mat-ripple="" cdkmonitorelementfocus="" class="mat-ripple mat-tab-label ng-star-inserted" id="mat-tab-label-0-2" tabindex="-1" aria-posinset="3" aria-setsize="7" aria-controls="mat-tab-content-0-2" aria-selected="false" aria-disabled="false"><div class="mat-tab-label-content"><span class="ng-star-inserted">app.component.css</span></div></div><div role="tab" mattablabelwrapper="" mat-ripple="" cdkmonitorelementfocus="" class="mat-ripple mat-tab-label ng-star-inserted" id="mat-tab-label-0-3" tabindex="-1" aria-posinset="4" aria-setsize="7" aria-controls="mat-tab-content-0-3" aria-selected="false" aria-disabled="false"><div class="mat-tab-label-content"><span class="ng-star-inserted">app.module.ts</span></div></div><div role="tab" mattablabelwrapper="" mat-ripple="" cdkmonitorelementfocus="" class="mat-ripple mat-tab-label ng-star-inserted" id="mat-tab-label-0-4" tabindex="-1" aria-posinset="5" aria-setsize="7" aria-controls="mat-tab-content-0-4" aria-selected="false" aria-disabled="false"><div class="mat-tab-label-content"><span class="ng-star-inserted">hero.ts</span></div></div><div role="tab" mattablabelwrapper="" mat-ripple="" cdkmonitorelementfocus="" class="mat-ripple mat-tab-label ng-star-inserted" id="mat-tab-label-0-5" tabindex="-1" aria-posinset="6" aria-setsize="7" aria-controls="mat-tab-content-0-5" aria-selected="false" aria-disabled="false"><div class="mat-tab-label-content"><span class="ng-star-inserted">hero-switch.components.ts</span></div></div><div role="tab" mattablabelwrapper="" mat-ripple="" cdkmonitorelementfocus="" class="mat-ripple mat-tab-label ng-star-inserted" id="mat-tab-label-0-6" tabindex="-1" aria-posinset="7" aria-setsize="7" aria-controls="mat-tab-content-0-6" aria-selected="false" aria-disabled="false"><div class="mat-tab-label-content"><span class="ng-star-inserted">unless.directive.ts</span></div></div></div><mat-ink-bar class="mat-ink-bar" style="visibility:visible;left:0;width:160px"></mat-ink-bar></div></div><div aria-hidden="true" mat-ripple="" class="mat-ripple mat-tab-header-pagination mat-tab-header-pagination-after mat-elevation-z4"><div class="mat-tab-header-pagination-chevron"></div></div></mat-tab-header><div class="mat-tab-body-wrapper"><mat-tab-body role="tabpanel" class="mat-tab-body ng-tns-c50-2 mat-tab-body-active ng-star-inserted" id="mat-tab-content-0-0" aria-labelledby="mat-tab-label-0-0"><div class="mat-tab-body-content ng-tns-c50-2 ng-trigger ng-trigger-translateTab" style="transform:none"><aio-code class="ng-star-inserted" style=""><pre class="lang- prettyprint">      <button title="Copy code snippet" class="material-icons copy-button no-print ng-star-inserted" aria-label="Copy code snippet from app.component.ts">
        <span aria-hidden="true">content_copy</span>
      </button>
      <code class="animated fadeIn"><span class="kwd">import</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><a href="api/core/Component" class="code-anchor"><span class="typ">Component</span></a><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">from</span><span class="pln"> </span><span class="str">'@angular/core'</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="typ">Hero</span><span class="pun">,</span><span class="pln"> heroes </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">from</span><span class="pln"> </span><span class="str">'./hero'</span><span class="pun">;</span><span class="pln">

</span><span class="lit">@</span><a href="api/core/Component" class="code-anchor"><span class="lit">Component</span></a><span class="pun">({</span><span class="pln">
  selector</span><span class="pun">:</span><span class="pln"> </span><span class="str">'app-root'</span><span class="pun">,</span><span class="pln">
  </span><a href="api/core/Component#templateUrl" class="code-anchor"><span class="pln">templateUrl</span></a><span class="pun">:</span><span class="pln"> </span><span class="str">'./app.component.html'</span><span class="pun">,</span><span class="pln">
  </span><a href="api/core/Component#styleUrls" class="code-anchor"><span class="pln">styleUrls</span></a><span class="pun">:</span><span class="pln"> </span><span class="pun">[</span><span class="pln"> </span><span class="str">'./app.component.css'</span><span class="pln"> </span><span class="pun">]</span><span class="pln">
</span><span class="pun">})</span><span class="pln">
</span><span class="kwd">export</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">AppComponent</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  heroes </span><span class="pun">=</span><span class="pln"> heroes</span><span class="pun">;</span><span class="pln">
  hero </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">heroes</span><span class="pun">[</span><span class="lit">0</span><span class="pun">];</span><span class="pln">

  condition </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">false</span><span class="pun">;</span><span class="pln">
  logs</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">string</span><span class="pun">[]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="pun">[];</span><span class="pln">
  showSad </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">true</span><span class="pun">;</span><span class="pln">
  status </span><span class="pun">=</span><span class="pln"> </span><span class="str">'ready'</span><span class="pun">;</span><span class="pln">

  </span><a href="api/core/IterableChangeRecord#trackById" class="code-anchor"><span class="pln">trackById</span></a><span class="pun">(</span><span class="pln">index</span><span class="pun">:</span><span class="pln"> number</span><span class="pun">,</span><span class="pln"> hero</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Hero</span><span class="pun">):</span><span class="pln"> number </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">return</span><span class="pln"> hero</span><span class="pun">.</span><span class="pln">id</span><span class="pun">;</span><span class="pln"> </span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span></code>
    </pre></aio-code></div></mat-tab-body><mat-tab-body role="tabpanel" class="mat-tab-body ng-tns-c50-3 ng-star-inserted" id="mat-tab-content-0-1" aria-labelledby="mat-tab-label-0-1"><div class="mat-tab-body-content ng-tns-c50-3 ng-trigger ng-trigger-translateTab" style="transform:translate3d(100%,0,0);min-height:1px"></div></mat-tab-body><mat-tab-body role="tabpanel" class="mat-tab-body ng-tns-c50-4 ng-star-inserted" id="mat-tab-content-0-2" aria-labelledby="mat-tab-label-0-2"><div class="mat-tab-body-content ng-tns-c50-4 ng-trigger ng-trigger-translateTab" style="transform:translate3d(100%,0,0);min-height:1px"></div></mat-tab-body><mat-tab-body role="tabpanel" class="mat-tab-body ng-tns-c50-5 ng-star-inserted" id="mat-tab-content-0-3" aria-labelledby="mat-tab-label-0-3"><div class="mat-tab-body-content ng-tns-c50-5 ng-trigger ng-trigger-translateTab" style="transform:translate3d(100%,0,0);min-height:1px"></div></mat-tab-body><mat-tab-body role="tabpanel" class="mat-tab-body ng-tns-c50-6 ng-star-inserted" id="mat-tab-content-0-4" aria-labelledby="mat-tab-label-0-4"><div class="mat-tab-body-content ng-tns-c50-6 ng-trigger ng-trigger-translateTab" style="transform:translate3d(100%,0,0);min-height:1px"></div></mat-tab-body><mat-tab-body role="tabpanel" class="mat-tab-body ng-tns-c50-7 ng-star-inserted" id="mat-tab-content-0-5" aria-labelledby="mat-tab-label-0-5"><div class="mat-tab-body-content ng-tns-c50-7 ng-trigger ng-trigger-translateTab" style="transform:translate3d(100%,0,0);min-height:1px"></div></mat-tab-body><mat-tab-body role="tabpanel" class="mat-tab-body ng-tns-c50-8 ng-star-inserted" id="mat-tab-content-0-6" aria-labelledby="mat-tab-label-0-6"><div class="mat-tab-body-content ng-tns-c50-8 ng-trigger ng-trigger-translateTab" style="transform:translate3d(100%,0,0);min-height:1px"></div></mat-tab-body></div></mat-tab-group></mat-card></code-tabs><p translation-result="on">你学到了</p><p translation-origin="off">You learned</p><ul><li><p translation-result="on">结构型指令可以操纵 HTML 的元素布局。</p><p translation-origin="off">that structural directives manipulate HTML layout.</p></li><li><p translation-result="on">当没有合适的宿主元素时，可以使用<a href="guide/structural-directives#ngcontainer"><code>&lt;ng-container&gt;</code></a>对元素进行分组。</p><p translation-origin="off">to use <a href="guide/structural-directives#ngcontainer"><code>&lt;ng-container&gt;</code></a>as a grouping element when there is no suitable host element.</p></li><li><p translation-result="on">Angular 会把<a href="guide/structural-directives#asterisk">星号（*）语法</a>解开成 <code>&lt;ng-template&gt;</code>。</p><p translation-origin="off">that the Angular desugars <a href="guide/structural-directives#asterisk">asterisk (*) syntax</a> into a <code>&lt;ng-template&gt;</code>.</p></li><li><p translation-result="on">内置指令 <code><a href="api/common/NgIf" class="code-anchor">NgIf</a></code>、<code>NgFor</code> 和 <code><a href="api/common/NgSwitch" class="code-anchor">NgSwitch</a></code> 的工作原理。</p><p translation-origin="off">how that works for the <code><a href="api/common/NgIf" class="code-anchor">NgIf</a></code>, <code>NgFor</code> and <code><a href="api/common/NgSwitch" class="code-anchor">NgSwitch</a></code> built-in directives.</p></li><li><p translation-result="on"><a href="guide/structural-directives#microsyntax"><em>微语法</em></a>如何展开成<a href="guide/structural-directives#template"><code>&lt;ng-template&gt;</code></a>。</p><p translation-origin="off">about the <a href="guide/structural-directives#microsyntax"><em>microsyntax</em></a> that expands into a <a href="guide/structural-directives#template"><code>&lt;ng-template&gt;</code></a>.</p></li><li><p translation-result="on">写了一个<a href="guide/structural-directives#unless">自定义结构型指令</a> —— <code>UnlessDirective</code>。</p><p translation-origin="off">to write a <a href="guide/structural-directives#unless">custom structural directive</a>, <code>UnlessDirective</code>.</p></li></ul></div></div></aio-doc-viewer></main></mat-sidenav-content></mat-sidenav-container><div class="toc-container no-print ng-star-inserted"><aio-lazy-ce selector="aio-toc"><aio-toc ng-version="9.0.0-rc.11"><div class="toc-inner no-print collapsed ng-star-inserted"><ul class="toc-list"><li class="h1 ng-star-inserted active" title="结构型指令"><a href="guide/structural-directives#structural-directives">结构型指令</a></li><li class="h2 ng-star-inserted" title="什么是结构型指令？"><a href="guide/structural-directives#what-are-structural-directives">什么是结构型指令？</a></li><li class="h2 ng-star-inserted" title="NgIf 案例分析"><a href="guide/structural-directives#ngif-case-study">NgIf 案例分析</a></li><li class="h3 ng-star-inserted" title="为什么移除而不是隐藏？"><a href="guide/structural-directives#why-remove-rather-than-hide">为什么<em>移除</em>而不是<em>隐藏</em>？</a></li><li class="h2 ng-star-inserted" title="星号（*）前缀"><a href="guide/structural-directives#the-asterisk--prefix">星号（*）前缀</a></li><li class="h2 ng-star-inserted" title="*ngFor 内幕"><a href="guide/structural-directives#inside-ngfor"><code>*ngFor</code> 内幕</a></li><li class="h2 ng-star-inserted" title="微语法"><a href="guide/structural-directives#microsyntax">微语法</a></li><li class="h3 ng-star-inserted" title="编写你自己的结构型指令"><a href="guide/structural-directives#writing-your-own-structural-directives">编写你自己的结构型指令</a></li><li class="h3 ng-star-inserted" title="约束"><a href="guide/structural-directives#constraints">约束</a></li><li class="h3 ng-star-inserted" title="语法"><a href="guide/structural-directives#grammar">语法</a></li><li class="h3 ng-star-inserted" title="翻译"><a href="guide/structural-directives#translation">翻译</a></li><li class="h3 ng-star-inserted" title="微语法样例"><a href="guide/structural-directives#microsyntax-examples">微语法样例</a></li><li class="h2 ng-star-inserted" title="模板输入变量"><a href="guide/structural-directives#template-input-variable">模板输入变量</a></li><li class="h2 ng-star-inserted" title="每个宿主元素上只能有一个结构型指令"><a href="guide/structural-directives#one-structural-directive-per-host-element">每个宿主元素上只能有一个结构型指令</a></li><li class="h2 ng-star-inserted" title="NgSwitch 内幕"><a href="guide/structural-directives#inside-ngswitch-directives"><code>NgSwitch</code> 内幕</a></li><li class="h2 ng-star-inserted" title="优先使用星号（*）语法"><a href="guide/structural-directives#prefer-the-asterisk--syntax">优先使用星号（<code>*</code>）语法</a></li><li class="h2 ng-star-inserted" title="<ng-template>元素"><a href="guide/structural-directives#the-ng-template"><em>&lt;ng-template&gt;</em>元素</a></li><li class="h2 ng-star-inserted" title="使用<ng-container>把一些兄弟元素归为一组"><a href="guide/structural-directives#group-sibling-elements-with-ng-container">使用&lt;ng-container&gt;把一些兄弟元素归为一组</a></li><li class="h3 ng-star-inserted" title="<ng-container> 的救赎"><a href="guide/structural-directives#ng-container-to-the-rescue">&lt;ng-container&gt; 的救赎</a></li><li class="h2 ng-star-inserted" title="写一个结构型指令"><a href="guide/structural-directives#write-a-structural-directive">写一个结构型指令</a></li><li class="h3 ng-star-inserted" title="TemplateRef 和 ViewContainerRef"><a href="guide/structural-directives#templateref-and-viewcontainerref"><em>TemplateRef</em> 和 <em>ViewContainerRef</em></a></li><li class="h3 ng-star-inserted" title="appUnless 属性"><a href="guide/structural-directives#the-appunless-property"><em>appUnless</em> 属性</a></li><li class="h2 ng-star-inserted" title="小结"><a href="guide/structural-directives#summary">小结</a></li></ul></div></aio-toc></aio-lazy-ce></div><footer class="no-print"><aio-footer><div class="grid-fluid"><div class="footer-block ng-star-inserted"><h3>资源</h3><ul><li class="ng-star-inserted"><a class="link" href="about" title="Angular 贡献者。">关于</a></li><li class="ng-star-inserted"><a class="link" href="resources" title="网络上的 Angular 工具、培训、博客等">资源列表</a></li><li class="ng-star-inserted"><a class="link" href="presskit" title="我们的联系方式、LOGO 和品牌">宣传资料</a></li><li class="ng-star-inserted"><a class="link" href="https://blog.angular.io/" title="Angular 官方博客">博客</a></li><li class="ng-star-inserted"><a class="link" href="analytics" title="Angular 使用情况分析">使用情况分析</a></li></ul></div><div class="footer-block ng-star-inserted"><h3>帮助</h3><ul><li class="ng-star-inserted"><a class="link" href="https://stackoverflow.com/questions/tagged/angular" title="Stack Overflow: 这里的社区会回答你关于 Angular 的技术问题">Stack Overflow</a></li><li class="ng-star-inserted"><a class="link" href="https://gitter.im/angular/angular" title="和老鸟聊 Angular">Gitter</a></li><li class="ng-star-inserted"><a class="link" href="https://github.com/angular/angular/issues" title="在 github 上报告问题和建议。">报告问题</a></li><li class="ng-star-inserted"><a class="link" href="https://github.com/angular/code-of-conduct/blob/master/CODE_OF_CONDUCT.md" title="让我们彼此尊重">行为规范</a></li></ul></div><div class="footer-block ng-star-inserted"><h3>社区</h3><ul><li class="ng-star-inserted"><a class="link" href="events" title="Angular events around the world.">活动</a></li><li class="ng-star-inserted"><a class="link" href="http://www.meetup.com/topics/angularjs/" title="参加聚会，向别的开发人员学习">聚会</a></li><li class="ng-star-inserted"><a class="link" href="https://twitter.com/angular" title="Twitter">Twitter</a></li><li class="ng-star-inserted"><a class="link" href="https://github.com/angular/angular" title="GitHub">GitHub</a></li><li class="ng-star-inserted"><a class="link" href="contribute" title="向 Angular 做贡献">做贡献</a></li></ul></div><div class="footer-block ng-star-inserted"><h3>多语言</h3><ul><li class="ng-star-inserted"><a class="link" href="https://angular.io/" title="English Version.">English Version</a></li><li class="ng-star-inserted"><a class="link" href="https://angular.tw/" title="正體中文版">正體中文版</a></li><li class="ng-star-inserted"><a class="link" href="https://angular.jp/" title="日本語版">日本語版</a></li><li class="ng-star-inserted"><a class="link" href="https://angular.kr/" title="한국어">한국어</a></li></ul></div></div><p>Super-powered by Google ©2010-2020. 代码授权方式：<a href="license" title="License text">MIT-style License</a>. 文档授权方式：<a href="http://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a>.</p><p>当前版本：9.0.1-build.109+sha.7a49faba.</p></aio-footer></footer><div class="cdk-visually-hidden ng-star-inserted"><mat-icon role="img" class="mat-icon notranslate material-icons mat-icon-no-color" aria-hidden="true">&nbsp;</mat-icon></div></aio-shell><noscript><div class="background-sky hero"></div><section id="intro" style="text-shadow:1px 1px #1976d2"><div class="hero-logo"><img src="assets/images/logos/angular/angular.svg" width="250" height="250" alt="Angular"></div><div class="homepage-container"><div class="hero-headline">一套框架，多种平台<br>移动 &amp; 桌面</div></div></section><h2 style="color:red;margin-top:40px;position:relative;text-align:center;text-shadow:1px 1px #fafafa"><b><i>该网站需要浏览器支持 JavaScript</i></b></h2></noscript><script src="runtime-es2015.811b066977719df5af39.js" type="module"></script><script src="runtime-es5.811b066977719df5af39.js" nomodule="" defer=""></script><script src="polyfills-es5.1494ca908613f3429457.js" nomodule="" defer=""></script><script src="polyfills-es2015.9057a05c650bf60ecbf4.js" type="module"></script><script src="main-es2015.4e9aa34b562bab1382ab.js" type="module"></script><script src="main-es5.4e9aa34b562bab1382ab.js" nomodule="" defer=""></script><div class="cdk-live-announcer-element cdk-visually-hidden" aria-atomic="true" aria-live="polite"></div></body></html>